Adding Column and column names dynamically in R









up vote
0
down vote

favorite












I have an R data.table like this:



id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14`


And I would like to add columns dynamically by presence of this Id in a row.
Result:



 id is_present_1 ... is_present_6....
1: 1 1 0
2: 2 0 0
3: 3 0 0
4: 4 0 1
5: 5 0 0
6: 6 0 1


I tried to write a function, or using mutate and paste:



ids <- c(1, 2, 3, 4, 5, 6)
for (i in length(ids))
df %>% mutate(paste("is_present",id[i]) = ifelse(id == ids[i],1,0))


I get an error:
Error: unexpected '=' in:
"for (i in length(ids)) {
df %>% mutate(paste("is_present",id[i]) ="



Can someone help with this?










share|improve this question





















  • You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
    – Tung
    Nov 12 at 7:47














up vote
0
down vote

favorite












I have an R data.table like this:



id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14`


And I would like to add columns dynamically by presence of this Id in a row.
Result:



 id is_present_1 ... is_present_6....
1: 1 1 0
2: 2 0 0
3: 3 0 0
4: 4 0 1
5: 5 0 0
6: 6 0 1


I tried to write a function, or using mutate and paste:



ids <- c(1, 2, 3, 4, 5, 6)
for (i in length(ids))
df %>% mutate(paste("is_present",id[i]) = ifelse(id == ids[i],1,0))


I get an error:
Error: unexpected '=' in:
"for (i in length(ids)) {
df %>% mutate(paste("is_present",id[i]) ="



Can someone help with this?










share|improve this question





















  • You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
    – Tung
    Nov 12 at 7:47












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an R data.table like this:



id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14`


And I would like to add columns dynamically by presence of this Id in a row.
Result:



 id is_present_1 ... is_present_6....
1: 1 1 0
2: 2 0 0
3: 3 0 0
4: 4 0 1
5: 5 0 0
6: 6 0 1


I tried to write a function, or using mutate and paste:



ids <- c(1, 2, 3, 4, 5, 6)
for (i in length(ids))
df %>% mutate(paste("is_present",id[i]) = ifelse(id == ids[i],1,0))


I get an error:
Error: unexpected '=' in:
"for (i in length(ids)) {
df %>% mutate(paste("is_present",id[i]) ="



Can someone help with this?










share|improve this question













I have an R data.table like this:



id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14`


And I would like to add columns dynamically by presence of this Id in a row.
Result:



 id is_present_1 ... is_present_6....
1: 1 1 0
2: 2 0 0
3: 3 0 0
4: 4 0 1
5: 5 0 0
6: 6 0 1


I tried to write a function, or using mutate and paste:



ids <- c(1, 2, 3, 4, 5, 6)
for (i in length(ids))
df %>% mutate(paste("is_present",id[i]) = ifelse(id == ids[i],1,0))


I get an error:
Error: unexpected '=' in:
"for (i in length(ids)) {
df %>% mutate(paste("is_present",id[i]) ="



Can someone help with this?







dynamic dplyr data.table






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 19:31









Jenny

103




103











  • You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
    – Tung
    Nov 12 at 7:47
















  • You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
    – Tung
    Nov 12 at 7:47















You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
– Tung
Nov 12 at 7:47




You need to use tidy evaluation to create dynamic names within dplyr verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
– Tung
Nov 12 at 7:47












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Not sure if I understand the question. But i think you are looking for a map function from purr with dynamic columns names. If the logic is wrong you can just adjust inside the function.



library(tidyverse)
library(data.table)

map_dfc(df$row, function(x)
nm <- paste("is_present_", x, sep = "")
df %>%
mutate(!!nm := ifelse(id == x, 1, 0))) %>%
select(contains("is_present_"))


results in:



 is_present_1 is_present_2 is_present_3 is_present_4 is_present_5 is_present_6
1 1 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 1
5 1 0 0 0 0 0
6 0 0 0 0 0 0


Sample data:



 df <- fread("
id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14") %>%
select(2) %>%
rownames_to_column("row")





share|improve this answer




















  • Thank you for your help :)
    – Jenny
    Nov 13 at 10:41










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252409%2fadding-column-and-column-names-dynamically-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Not sure if I understand the question. But i think you are looking for a map function from purr with dynamic columns names. If the logic is wrong you can just adjust inside the function.



library(tidyverse)
library(data.table)

map_dfc(df$row, function(x)
nm <- paste("is_present_", x, sep = "")
df %>%
mutate(!!nm := ifelse(id == x, 1, 0))) %>%
select(contains("is_present_"))


results in:



 is_present_1 is_present_2 is_present_3 is_present_4 is_present_5 is_present_6
1 1 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 1
5 1 0 0 0 0 0
6 0 0 0 0 0 0


Sample data:



 df <- fread("
id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14") %>%
select(2) %>%
rownames_to_column("row")





share|improve this answer




















  • Thank you for your help :)
    – Jenny
    Nov 13 at 10:41














up vote
0
down vote



accepted










Not sure if I understand the question. But i think you are looking for a map function from purr with dynamic columns names. If the logic is wrong you can just adjust inside the function.



library(tidyverse)
library(data.table)

map_dfc(df$row, function(x)
nm <- paste("is_present_", x, sep = "")
df %>%
mutate(!!nm := ifelse(id == x, 1, 0))) %>%
select(contains("is_present_"))


results in:



 is_present_1 is_present_2 is_present_3 is_present_4 is_present_5 is_present_6
1 1 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 1
5 1 0 0 0 0 0
6 0 0 0 0 0 0


Sample data:



 df <- fread("
id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14") %>%
select(2) %>%
rownames_to_column("row")





share|improve this answer




















  • Thank you for your help :)
    – Jenny
    Nov 13 at 10:41












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Not sure if I understand the question. But i think you are looking for a map function from purr with dynamic columns names. If the logic is wrong you can just adjust inside the function.



library(tidyverse)
library(data.table)

map_dfc(df$row, function(x)
nm <- paste("is_present_", x, sep = "")
df %>%
mutate(!!nm := ifelse(id == x, 1, 0))) %>%
select(contains("is_present_"))


results in:



 is_present_1 is_present_2 is_present_3 is_present_4 is_present_5 is_present_6
1 1 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 1
5 1 0 0 0 0 0
6 0 0 0 0 0 0


Sample data:



 df <- fread("
id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14") %>%
select(2) %>%
rownames_to_column("row")





share|improve this answer












Not sure if I understand the question. But i think you are looking for a map function from purr with dynamic columns names. If the logic is wrong you can just adjust inside the function.



library(tidyverse)
library(data.table)

map_dfc(df$row, function(x)
nm <- paste("is_present_", x, sep = "")
df %>%
mutate(!!nm := ifelse(id == x, 1, 0))) %>%
select(contains("is_present_"))


results in:



 is_present_1 is_present_2 is_present_3 is_present_4 is_present_5 is_present_6
1 1 0 0 0 0 0
2 0 0 0 0 0 0
3 0 0 0 0 0 0
4 0 0 0 0 0 1
5 1 0 0 0 0 0
6 0 0 0 0 0 0


Sample data:



 df <- fread("
id
1: 1
2: 29
3: 26
4: 6
5: 1
6: 14") %>%
select(2) %>%
rownames_to_column("row")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 18:59









davsjob

49726




49726











  • Thank you for your help :)
    – Jenny
    Nov 13 at 10:41
















  • Thank you for your help :)
    – Jenny
    Nov 13 at 10:41















Thank you for your help :)
– Jenny
Nov 13 at 10:41




Thank you for your help :)
– Jenny
Nov 13 at 10:41

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252409%2fadding-column-and-column-names-dynamically-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党