Edu
Edu

Reputation: 903

Create a sublist within a list of data frames in terms of the dimension of the data frames

I have a list of data frames that have different dimensions. I want to create different alternative sublists that contain data frames with the same number of columns.

The structure of my list df_list looks something like this:

List of 6

  $ df1:'data.frame':   49743 obs. of  88 variables
  $ df2:'data.frame':   49889 obs. of  89 variables 
  $ df3:'data.frame':   50500 obs. of  91 variables
  $ df4:'data.frame':   49732 obs. of  88 variables
  $ df5:'data.frame':   48500 obs. of  90 variables 
  $ df6:'data.frame':   50011 obs. of  91 variables

My desired output would be something similar to:

sub_list1 = list(df1, df4)
sub_list2 = list(df3, df6)

Could anyone help me to solve this issue? Many thanks in advance

Upvotes: 2

Views: 993

Answers (1)

talat
talat

Reputation: 70296

It's very easily solved using

split(df_list, lengths(df_list)) 
# or for older R versions: split(df_list, sapply(df_list, ncol))

which will result in a new list of lists and each of the sublists contains data.frame's with equal numbers of columns.


Here's a reproducible example:

l <- list(
  data.frame(x = 1),
  data.frame(x = 1, y = 2),
  data.frame(x = 1),
  data.frame(x = 1, y = 2, z = 3),
  data.frame(x = 1))

To check how many variables each data.frame in l has, run:

lengths(l)
#[1] 1 2 1 3 1

Now you can split them and check the structure:

res <- split(l, lengths(l))
str(res)
#List of 3
# $ 1:List of 3
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
#  ..$ :'data.frame':   1 obs. of  1 variable:
#  .. ..$ x: num 1
# $ 2:List of 1
#  ..$ :'data.frame':   1 obs. of  2 variables:
#  .. ..$ x: num 1
#  .. ..$ y: num 2
# $ 3:List of 1
#  ..$ :'data.frame':   1 obs. of  3 variables:
#  .. ..$ x: num 1
#  .. ..$ y: num 2
#  .. ..$ z: num 3

Upvotes: 2

Related Questions