我有一个从运行grep()
创建的矢量命名列表。我想找到那些没有找到任何价值的元素,或者找到了一堆NA的元素。我该怎么做?
我试过了,但没有走得太远。
MyList<-c("Hello World", "a123 234","Hi world","a12345678 09876","publ school")
look_New<-c("world","a123","a1234","121314","pub school",NA) #this was generated using different code.
look_Old<-c("a","b","c","d","e","f") #this was generated using different code.
Look<-data.frame(look_New,look_Old,stringsAsFactors=FALSE)
Result<-lapply(Look$look_New, grep, x = MyList,ignore.case=TRUE)
names(Result)<-Look$look_Old
#which elements are null?
Look$look_Old[which(sapply(Result,length)==0)]
2个问题:
问题:1)
问题是上面的代码没有告诉我f
有一堆NA
s。但是,它确实显示了d
和e
预期输出为:
d
e
f
问题2:
无论如何我可以用零向量替换这么多的NA吗?即如果我运行Results$f
,它应显示在下方(正如我通过运行Results$e
得到的那样:
Result$f
integer(0)
非常感谢任何帮助。
答案 0 :(得分:1)
我们可以尝试
v1 <- sapply(Result, function(x) length(x)==0 | all (is.na(x)))
names(v1)[v1]
#[1] "d" "e" "f"
或使用Filter
names(Filter(function(x) !length(x)|all(is.na(x)), Result))
答案 1 :(得分:1)
删除lengths()
值后使用NA
:
names(Result)[!lengths(lapply(Result, na.omit))]
#[1] "d" "e" "f"