var1 is a list:
var1 <- list(c("tall tree", "fruits", "star"),
c("tree tall", "pine tree", "tree pine", "black forest", "water"),
c("apple", "orange", "grapes"),
c("ancient pine tree", "all trees"))
I need to remove those elements entirely from the list which contains the term "pine".
The desired answer is a list:
[[1]]
[1] "tall tree" "fruits" "star"
[[2]]
[1] "apple" "orange" "grapes"
Thanks
You could try Filter
here
Filter(function(x) !any(grepl("pine", x)), var1)
# [[1]]
# [1] "tall tree" "fruits" "star"
#
# [[2]]
# [1] "apple" "orange" "grapes"
var1[lapply(var1,function(x) length(grep("pine",x,value=FALSE))) == 0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With