[R] length with missing values

Petr Pikal petr.pikal at precheza.cz
Fri Oct 15 14:00:18 CEST 2004



On 15 Oct 2004 at 12:01, Luis Rideau Cruz wrote:

> R-help
> 
> I have a martix with missing values( in which I want the sample size
> by column) When I :
> 
> apply(matrix,2,length)

Hi

Try
apply(matrix, 2, function(x) sum(!is.na(x)))

matrix is reserved word, so it is preferable to use other name for 
your object.

Cheers
Petr

> 
> I get the length of the vector regardless of missing values.
> I can't pass an argument to length in apply.
> 
> Alternatively I could 
> 
> ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1)
> 
> Is there any easier way?
> 
> Thank you
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list