[R] number of an element in a matrix

Roland Rau roland.rproject at gmail.com
Mon Aug 11 23:18:10 CEST 2008


Hi,

Brandon Invergo wrote:
> You can use the length() and which() functions for that:
> 
>  > length(which(m == 2))
Or even shorter since TRUE Values have a value of 1

mymatrix <- matrix(sample(x=1:10, size=1000, replace=TRUE), ncol=5)
sum(mymatrix==2)
length(which(mymatrix==2)) #should give the same result

Best,
Roland



More information about the R-help mailing list