Filter values from list in R

偶尔善良 提交于 2019-12-03 11:59:41
## Example list

l <- list(n1=numeric(0), n2="foo", n3=numeric(0), n4=1:5)

## Filter out elements with length 0

l[lapply(l, length) > 0]


$n2
[1] "foo"

$n4
[1] 1 2 3 4 5


## Get names of elements with length 0

names(l)[lapply(l, length) == 0]

[1] "n1" "n3"
Ramnath

Another solution using the Filter function from base R:

mean(unlist(Filter(is.numeric, l)))

Unlisting will pull out just the numeric entries in a single vector which you can call mean on, so try:

mean(unlist(r["gg",]))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!