我试图复制这个解决方案的成功:
remove columns with NAs from all dataframes in list
或
Remove columns from dataframe where some of values are NA
包含数据框列表:
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。我猜这会导致“未定义列”错误,但我不确定。
非常感谢任何帮助或建议。
此致
答案 0 :(得分:2)
我认为你没有看到正确的问题和答案。
请参阅Remove columns from dataframe where ALL values are NA,这似乎是您想要的。
然后,您可以修改my answer以提供
lapply(snag, Filter, f = function(x){!all(is.na(x))})
$v1520
vPWMETRO vPWPUMA00 v100 v101
26 1520 500 96.16667 5
27 1520 900 71.46154 15
28 1520 1000 68.63636 NA
29 1520 1100 22.50000 NA
30 1520 1200 64.50000 NA
$v6440
vPWMETRO vPWPUMA00 v100 v102
39 6440 1300 38.39216 38.33333
40 6440 2100 35.00000 68.00000
41 6440 2200 12.50000 NA
NA NA NA NA NA
NA.1 NA NA NA NA