简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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