2

I currently have a list of 7 dataframes, each of which have 24 columns, but not the same number of rows. I would like to convert my list to a 3-dimension array, but I can't because all the components of my list do not have the same dimension. I have one dataframe with 60 rows, 4 dataframes with 59 rows, and 2 with 58 rows.

When I try laply(mylist, unlist), I get the following message: Error: Results must have the same dimensions.

Is there any way to put those dataframes into an array? How could I get to put NAs at the end of the 6 other dataframes in order to get them to 60 rows?

Alex
  • 23
  • 4
  • 1
    This should get you most of the way there: https://stackoverflow.com/questions/34570860/adding-na-to-make-all-list-elements-equal-length – HFBrowning Jun 08 '18 at 22:28

1 Answers1

2

I'm not sure about real purpose of OP which lead him to think of creating a 3-D array, for which he needs all data frames of list containing same number of of rows.

But, whatever the reason be, one can achieve it using lapply. Please make a note that lengths function doesn't work properly on list containing data frames. As lengths function simply returns number of columns in each data frame contained in list.

Hence, the approach is to first find maximum number of rows in a data frame contained in mylist. And then iterate over each data frame to extend its rows to maximum number of rows.

# Find maximum row across all data frames in mylist
maxrow <- max(sapply(mylist, nrow))

# Iterate over and increase the row count to maxrow
mylist_mod <- lapply(mylist, function(x,nRow){
                if(nrow(x) <  nRow){
                  x[(nrow(x)+1):nRow,] <- NA
                }
                x
              }, nRow = maxrow)

mylist_mod
# $df1
#   one two three
# 1 101 111   131
# 2 102 112   132
# 3 103 113   133
# 4  NA  NA    NA
# 5  NA  NA    NA
# 
# $df2
#   one two three
# 1 201 211   231
# 2 202 212   232
# 3  NA  NA    NA
# 4  NA  NA    NA
# 5  NA  NA    NA
# 
# $df3
#   one two three
# 1 301 311   331
# 2 302 312   332
# 3 303 313   333
# 4 304 314   334
# 5 305 315   335

Sample Data:

df1 <- data.frame(one = 101:103, two = 111:113, three = 131:133)
df2 <- data.frame(one = 201:202, two = 211:212, three = 231:232)
df3 <- data.frame(one = 301:305, two = 311:315, three = 331:335)

mylist <- list(df1 = df1, df2 = df2, df3 = df3)

mylist 
# $df1
#   one two three
# 1 101 111   131
# 2 102 112   132
# 3 103 113   133
# 
# $df2
#   one two three
# 1 201 211   231
# 2 202 212   232
# 
# $df3
#   one two three
# 1 301 311   331
# 2 302 312   332
# 3 303 313   333
# 4 304 314   334
# 5 305 315   335
MKR
  • 19,739
  • 4
  • 23
  • 33