Re: [R] run function on subsets of matrix

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 27 Mar 2011 02:25:16 -0400

On Mar 26, 2011, at 10:26 PM, fisken wrote:

> I was wondering if it is possible to do the following in a smarter
> way.
>
> I want get the mean value across the columns of a matrix, but I want
> to do this on subrows of the matrix, given by some vector(same length
> as the the number of rows). Something like
>
> nObs<- 6
> nDim <- 4
> m <- matrix(rnorm(nObs*nDim),ncol=nDim)
> fac<-sample(1:(nObs/2),nObs,rep=T)
>
> ##loop trough different 'factor' levels
> for (i in unique(fac))
> print(apply(m[fac==i,],2,mean))

This would be a lot simpler and faster:

  colMeans(m[unique(fac),])

#[1] 1.3595197 -0.1374411 0.1062527 -0.3897732

>
> Now, the problem is that if a value in 'fac' only occurs once, the
> 'apply' function will complain.

Because "[" will drop single dimensions and so the matrix becomes a vector and looses the number-2 margin. Use drop=FALSE to prevent this, and note the extra comma:

print(apply(m[1, , drop=FALSE],2,mean))

-- 

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 27 Mar 2011 - 06:30:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 27 Mar 2011 - 08:00:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive