I have a data frame df whose first column is a character vector and the rest numeric.
Example data frame:
df <- data.frame(my_names=sample(LETTERS,4,replace=F),
column2=sample(1.3:100.3,4,replace=T),
column3=sample(1.3:100.3,4,replace=T),
column4=sample(1.3:100.3,4,replace=T),
column5=sample(1.3:100.3,4,replace=T))
> df
my_names column2 column3 column4 column5
1 A 8.3 1.3 19.3 91.3
2 E 18.3 42.3 8.3 76.3
3 O 6.3 46.3 26.3 91.3
4 M 73.3 6.3 59.3 93.3
Now I want to create 4 different data frames like this:
- d1: my_names & column2
- d2: my_names & column3
- d3: my_names & column4
- d4: my_names & column5
And store them into a list.
d1 would look like:
> d1
my_names column2
1 A 8.3
2 E 18.3
3 O 6.3
4 M 73.3
I have tried:
>the_list <- vector("list",ncol(df)-1)
> for(i in 1:length(the_list)){ for(j in 2:ncol(df)){
+ the_list[[i]] <- select(df, my_names,j)
+ }
+ }
Note: Using an external vector in selections is ambiguous.
ℹ Use `all_of(j)` instead of `j` to silence this message.
But I get a list where all data frames are with column5:
> str(the_list)
List of 4
$ :'data.frame': 4 obs. of 2 variables:
..$ my_names: chr [1:4] "A" "E" "O" "M"
..$ column5 : num [1:4] 91.3 76.3 91.3 93.3
$ :'data.frame': 4 obs. of 2 variables:
..$ my_names: chr [1:4] "A" "E" "O" "M"
..$ column5 : num [1:4] 91.3 76.3 91.3 93.3
$ :'data.frame': 4 obs. of 2 variables:
..$ my_names: chr [1:4] "A" "E" "O" "M"
..$ column5 : num [1:4] 91.3 76.3 91.3 93.3
$ :'data.frame': 4 obs. of 2 variables:
..$ my_names: chr [1:4] "A" "E" "O" "M"
..$ column5 : num [1:4] 91.3 76.3 91.3 93.3
I take the recommendation from the error (using all_of(j)) and write:
> for(i in 1:length(the_list)){
for(j in 2:ncol(df)){
the_list[[i]] <- select(df, my_names,all_of(j))
}
}
But the result is the same as above.
I have read that one could use split, but I have nothing to group by, it´s each column separately.
e.g this does not work:
new_list<-list(split(df, colnames(df))
I get a wird list of 1.