我有以下行正确地设置了一个字符列表:
> cpc_data2[[1]]
[1] "" "Week" "" "" "" "" "" "" "" ""
[11] "" "SST" "SSTA" "" "" "" "" "SST" "SSTA" ""
[21] "" "" "" "SST" "SSTA" "" "" "" "" "SST"
[31] "SSTA"
> cpc_data2[[1]][which(cpc_data2[1][[1]] != "")]
[1] "Week" "SST" "SSTA" "SST" "SSTA" "SST" "SSTA" "SST" "SSTA"
我想在cpc_data2
中对每个列表进行子集化。我该怎么做?我尝试了以下内容,显然我的语法不正确:
> cpc_data3 = lapply(cpc_data2, function(x) x[which(x[[1]] != "")])
> head(cpc_data3)
[[1]]
character(0)
[[2]]
character(0)
[[3]]
character(0)
答案 0 :(得分:1)
你可以尝试
lapply(cpc_data2, function(x) x[x!=''])