Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split dataframe into several data frames within a list, each column separately

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.

like image 862
Paula Avatar asked Nov 24 '25 18:11

Paula


1 Answers

Try this tidyverse approach. You can format your data to long to transform columns into rows. Then, with split() you can create a list based on the column name. Finally, you can apply a function to transform your data to wide at each dataframe in the list and reach the desired output. Here the code:

library(tidyverse)
#Data
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))
#Reshape to long
df2 <- df %>% pivot_longer(cols = -1)
#Split into a list
List <- split(df2,df2$name)
#Now reshape function for wide format
List2 <- lapply(List,function(x){x<-pivot_wider(x,names_from = name,values_from = value);return(x)})
names(List2) <- paste0('df',1:length(List2))

Output:

List2
$df1
# A tibble: 4 x 2
  my_names column2
  <fct>      <dbl>
1 N           21.3
2 H           35.3
3 X           42.3
4 U           89.3

$df2
# A tibble: 4 x 2
  my_names column3
  <fct>      <dbl>
1 N           94.3
2 H           54.3
3 X            2.3
4 U           38.3

$df3
# A tibble: 4 x 2
  my_names column4
  <fct>      <dbl>
1 N           75.3
2 H           94.3
3 X           87.3
4 U          100. 

$df4
# A tibble: 4 x 2
  my_names column5
  <fct>      <dbl>
1 N           60.3
2 H           88.3
3 X           14.3
4 U           99.3
like image 164
Duck Avatar answered Nov 26 '25 07:11

Duck