如何从R中的列表中删除NULL值?
值NULL用于表示对象,尤其是长度为零的列表。如果列表包含NULL,那么我们可能希望将其替换为另一个值,或者如果我们没有任何替换值,则将其从列表中删除。要从列表中删除NULL值,我们可以将sapply与is.NULL取反。
例子
x<-list(rep(10,5),NULL,12,"Nhooo",2006,NULL,NULL,NULL,TRUE,c(rep(1,5),rep(2,5),NULL,"Education","Video Lectures","Online",NULL,"Over Million Visitors"))x
[[1]]
[1] 10 10 10 10 10
[[2]]
NULL
[[3]]
[1] 12
[[4]]
[1] "Nhooo"
[[5]]
[1] 2006
[[6]]
NULL
[[7]]
NULL
[[8]]
NULL
[[9]]
[1] TRUE
[[10]]
[1] "1" "1" "1"
[4] "1" "1" "2"
[7] "2" "2" "2"
[10] "2" "Education" "Video Lectures"
[13] "Online" "Over Million Visitors"
x<-x[!sapply(x,is.null)]
x
[[1]]
[1] 10 10 10 10 10
[[2]]
[1] 12
[[3]]
[1] "Nhooo"
[[4]]
[1] 2006
[[5]]
[1] TRUE
[[6]]
[1] "1" "1" "1"
[4] "1" "1" "2"
[7] "2" "2" "2"
[10] "2" "Education" "Video Lectures"
[13] "Online" "Over Million Visitors"
y<-list(c(5,10,15),NULL,c(rep("A",4)),rep(5,15,10,times=4),c(78,91,56,47,58,26,36,74,84),NULL,NULL,NULL)
y
[[1]]
[1] 5 10 15
[[2]]
NULL
[[3]]
[1] "A" "A" "A" "A"
[[4]]
[1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
[[5]]
[1] 78 91 56 47 58 26 36 74 84
[[6]]
NULL
[[7]]
NULL
[[8]]
NULL
y<-y[!sapply(y,is.null)]
y
[[1]]
[1] 5 10 15
[[2]]
[1] "A" "A" "A" "A"
[[3]]
[1] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
[[4]]
[1] 78 91 56 47 58 26 36 74 84
以上是 如何从R中的列表中删除NULL值? 的全部内容, 来源链接: utcz.com/z/357575.html