Reputation: 542
I have this list of data-frames,
[[1]]
ns title missing
1 0 another string to fail
[[2]]
pageid ns title
1 40657 0 Action
[[3]]
pageid ns title
1 8609 0 Nationalisme
I would like to have a script to remove all ranks that don't have "pageid", "ns" ans "title" as colnames.
Thank you by advance
Upvotes: 1
Views: 7636
Reputation: 887028
We can use %in%
and all
to create a logical vector
by looping through the list
elements with sapply
. The logical vector
can be used to subset the list
.
lst[sapply(lst, function(x) all(c("pageid", "ns", "title") %in% names(x)))]
#[[1]]
# pageid ns title
#1 40657 0 Action
#[[2]]
# pageid ns title
#1 8609 0 Nationalisme
Or we can use Filter
Filter(function(x) all(c("pageid", "ns", "title") %in% names(x)), lst)
lst <- list(data.frame(ns = 0, title = "another string", missing = "to fail"),
data.frame(pageid = 40657, ns =0, title = "Action"),
data.frame(pageid= 8609, ns=0, title = "Nationalisme"))
Upvotes: 6