How many non-NA values in each row for a matrix?

六眼飞鱼酱① 提交于 2019-12-02 10:18:05

Most Simply:

rowSums(!is.na(x)) (thanks to @Khashaa for this code).

Note the use of ! which equates to "not". This means that !is.na(x) is evaluating the statement "values that are not equal to "NA".

Alternatively:

To return not NA you can change the code as follows:

sum(is.na(x)==FALSE)

You can modify the code using apply to apply the code over the matrix as follows:

apply(d,2,function(x) sum(is.na(x))==TRUE))

where d is a matrix such as:

d=matrix(c(1,NA,NA,NA),ncol=2,nrow=2)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!