我正在尝试复制此解决方案的成功:
或者
带有数据框列表:
m1<- structure(list(vPWMETRO = c(1520L, 1520L, 1520L, 1520L, 1520L),
vPWPUMA00 = c(500L, 900L, 1000L, 1100L, 1200L),
v100 = c(96.1666666666667, 71.4615384615385, 68.6363636363636, 22.5, 64.5),
v101 = c(5, 15, NA, NA, NA),
v102 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)),
.Names = c("vPWMETRO", "vPWPUMA00", "v100", "v101", "v102"),
row.names = 26:30, class = "data.frame")
m2<- structure(list(vPWMETRO = c(6440L, 6440L, 6440L, NA, NA),
vPWPUMA00 = c(1300L,2100L, 2200L, NA, NA),
v100 = c(38.3921568627451, 35, 12.5, NA, NA),
v101 = c(NA, NA, NA, NA, NA),
v102 = c(38.3333333333333, 68, NA, NA, NA)),
.Names = c("vPWMETRO", "vPWPUMA00", "v100", "v101", "v102"),
row.names = c("39", "40", "41", "NA", "NA.1"), class = "data.frame")
#views structure
str(m1)
str(m2)
#creates list
snag<- list(v1520=m1, v6440=m2)
str(snag)
#attempts lapply solution
prob1<- lapply(snag, function(y) y[ ,!is.na(y)])
#2nd attempt, same result on just dataframe:
x5$v6440[ , apply(x5$v6440, 2, function(x) !(is.na(x)))]
这样包含所有 NA 的列都会在数据框中删除。因此结果应该是 2 个 df 的列表:
v1520:vPWPUMA00、v100、v101
v6440:vPWPUMA00、v100、v102
我看到示例问题的不同之处在于尺寸是 1x11,而我的尺寸是 5x5。我猜这会导致“未定义的列”错误,但我不确定。
任何帮助或建议将不胜感激。
问候,