This post How do I remove empty data frames from a list? talks about removing empty dataframes. How do i remove empty dataframes(nrow =0) from a list and replace them with 1 row placeholder dataframes/data.tables?
M1 <- data.frame(matrix(1:4, nrow = 2, ncol = 2))
M2 <- data.frame(matrix(nrow = 0, ncol = 0))
M3 <- data.frame(matrix(9:12, nrow = 2, ncol = 2))
mlist <- list(M1, M2, M3)
placeholder = data.table(a=1,b=1)
lapply(mlist, function(x) ifelse(nrow(fundslist[[x]]) == 0, placeholder, x))
One option would be using lengths
mlist[!lengths(mlist)] <- list(placeholder)
str(mlist)
#List of 3
# $ :'data.frame': 2 obs. of 2 variables:
# ..$ X1: int [1:2] 1 2
# ..$ X2: int [1:2] 3 4
# $ :Classes ‘data.table’ and 'data.frame': 1 obs. of 2 variables:
# ..$ a: num 1
# ..$ b: num 1
# ..- attr(*, ".internal.selfref")=<externalptr>
# $ :'data.frame': 2 obs. of 2 variables:
# ..$ X1: int [1:2] 9 10
# ..$ X2: int [1:2] 11 12