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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…