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?
dynamic dplyr data.table
add a comment |
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?
dynamic dplyr data.table
You need to use tidy evaluation to create dynamic names withindplyr
verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
– Tung
Nov 12 at 7:47
add a comment |
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?
dynamic dplyr data.table
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
dynamic dplyr data.table
asked Nov 11 at 19:31
Jenny
103
103
You need to use tidy evaluation to create dynamic names withindplyr
verbs. See some examples here: stackoverflow.com/q/49700912/786542 & maraaverick.rbind.io/2017/08/tidyeval-resource-roundup
– Tung
Nov 12 at 7:47
add a comment |
You need to use tidy evaluation to create dynamic names withindplyr
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
add a comment |
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")
Thank you for your help :)
– Jenny
Nov 13 at 10:41
add a comment |
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")
Thank you for your help :)
– Jenny
Nov 13 at 10:41
add a comment |
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")
Thank you for your help :)
– Jenny
Nov 13 at 10:41
add a comment |
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")
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")
answered Nov 12 at 18:59
davsjob
49726
49726
Thank you for your help :)
– Jenny
Nov 13 at 10:41
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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