[R] strange use of sapply

Liaw, Andy andy_liaw at merck.com
Tue Jun 20 22:31:16 CEST 2006


sapply() is not the right tool.  It operates on a list, and a matrix is not
a list (at least not treated as you'd expected it to be).  It would have
sort of worked if tempMatrix were a data frame instead of a matrix.

Try something like:

colSums(tempMatrix[, seq(1, ncol(tempMatrix), by=2)] > 0)

Andy 

From: markleeds at verizon.net
> 
> I've tried and give up. I have a matrix of say 200 columns 
> and 400 rows.
> 
> For each odd ( or even i suppose if i wanted to )column, I 
> want to know the number of rows in which the value is greater 
> than zero.
> 
> So, I did sapply(tempMatrix,2,function(x) sum( x > 0 ))
> 
> this almost works but i don't know how to tell it to only do 
> the odd columns. my guess is , like everything else in R, 
> this is possible. Thanks.
> 
> ______________________________________________
> 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
> 
>



More information about the R-help mailing list