[R] Trying to eliminate a for loop

Steven Wolf wolfste4 at msu.edu
Wed Feb 29 22:51:36 CET 2012

Yes!  That works.  

Thank you so much!


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Petr Savicky
Sent: Wednesday, February 29, 2012 4:41 PM
To: r-help at r-project.org
Subject: Re: [R] Trying to eliminate a for loop

On Wed, Feb 29, 2012 at 03:52:15PM -0500, Steven Wolf wrote:
> I keep trying to eliminate for loops when I arrive at them, but this 
> one is stumping me.  What is the nifty way to do this?
> My object data.cca is the output of the cancor function (for some two 
> datasets X and Y) (data.cca is a numeric vector)
> data.cca <- cancor(X,Y)
> Xcen=0*X
> for(i in 1:dim(X)[1]){
> 	Xcen[i,]=data.cca$xcenter
> 	}
> Xc = X - Xcen


Is the following, what you are looking for?

  Xc <- sweep(X, 2, data.cca$xcenter)

Hope this helps.

Petr Savicky.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list