[R] Analog to matlab repmat function

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 29 03:50:53 CEST 2006


Check out ?sweep

On 5/28/06, Daniil Ivanov <daniil.ivanov at gmail.com> wrote:
> Oops, mistake, should be
>
> data = data - repmat(mean(data,1),[130 1 1 1]);
>
> Sorry.
>
> On 5/29/06, Daniil Ivanov <daniil.ivanov at gmail.com> wrote:
> > Hello,
> >
> >  I'm trying to switch from Matlab to R-project, and having some difficulties.
> >  I make a use of multidimensional matrices. For example, I need to extract
> >  mean from one of the dimensions:
> >
> >  % we have matrix data of size: 130 x 11 x 350 x 2
> >  data = data - repmat(mean(data,3),[130 1 1 1]);
> >
> >  In R project I managed to do that in a very pervarsive way:
> >
> >  # mean(data,3) in R
> >  base <- apply(data,c(2,3,4),mean)
> >  # repmat(...,[130 1 1 1])
> >  base <- rep(base,130)
> >  dim(base) <- c(11,2,350,130)
> >  base <- aperm(base,c(4,1,2,3))
> >  # data = data - repmat
> >  data <- data - base;
> >
> >  Could you please show me a more elegant way of doing the same in R :)
> >
> > Thanks, Daniil.
> >
>
> ______________________________________________
> 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