简体   繁体   English

如何对具有混合数据类型和数据结构的列表进行子集设置?

[英]How do I subset a list with mixed data type and data structure?

I have a list which included a mix of data type (character) and data structure (dataframe). 我有一个列表,其中包含数据类型(字符)和数据结构(数据帧)的混合。

I want to keep only the dataframes and remove the rest. 我只想保留数据框并删除其余的。

> head(list)

[[1]]
[1] "/Users/Jane/R/12498798.txt error"

[[2]]
                                                                                                                                            match
1  Japan arrests man for taking gun
2                                                                                                              Extradition bill turns ugly
                                                     file
1 /Users/Jane/R/12498770.txt
2 /Users/Jane/R/12498770.txt

[[3]]
[1] "/Users/Jane/R/12498780.txt error"

I expect the final list to contain only dataframes: 我希望最终列表仅包含数据帧:

[[2]]
                                                                                                                                            match
1  Japan arrests man for taking gun
2                                                                                                              Extradition bill turns ugly
                                                     file
1 /Users/Jane/R/12498770.txt
2 /Users/Jane/R/12498770.txt

根据示例,OP的list元素可能是vectors并且想要删除any具有“错误”子字符串的元素

list[!sapply(list, function(x) any(grepl("error$", x)))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM