[R] Using a loop to define new variables

Gabor Grothendieck ggrothendieck at gmail.com
Fri May 28 14:23:27 CEST 2010


Although you asked for a loop this may be clearer if you just have to
do it once and only have 10 x and 10 y columns.  Here we use the built
in anscombe data frame which has columns x1, x2, x3, x4, y1, y2, y3,
y4:

  transform(anscombe, r1 = y1 / x1, r2 = y2 / x2, r3 = y3 / x3, r4 = y4 / x4)

In the future please provide sample data with your posts.

On Fri, May 28, 2010 at 7:52 AM, Andre Easom <AEasom at sportingindex.com> wrote:
> Hi,
>
> I'm a novice R user, much more used to SAS.  My problem is pretty simple - basically, in a data frame, I have variables named
> x1,....,x10 and y1,...,y10; and I would like to create r1 = x1 / y1 etc
>
> Apologies if this is way too rudimentary - but I couldn't find any posts online which solve this exact issue.
>
> Cheers,
> Andre
> **********************************************************************
> This email and any attachments are confidential, protect...{{dropped:22}}
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list