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