gib*_*z00 1 r apply lapply dplyr data.table
这篇文章如何从列表中删除空数据帧?谈论删除空数据帧.如何从列表中删除空数据帧(nrow = 0)并将其替换为1行占位符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)
Run Code Online (Sandbox Code Playgroud)
我试过了:
lapply(mlist, function(x) ifelse(nrow(fundslist[[x]]) == 0, placeholder, x))
Run Code Online (Sandbox Code Playgroud)
一种选择是使用 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
Run Code Online (Sandbox Code Playgroud)