使用lapply对R中的字符列表列表进行子集化

时间:2015-01-28 08:07:09

标签: r

我有以下行正确地设置了一个字符列表:

> 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)

1 个答案:

答案 0 :(得分:1)

你可以尝试

lapply(cpc_data2, function(x) x[x!=''])