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}}
Hi, Would this do: test <- data.frame(a=LETTERS[1:10], x=1:10, y=seq(0.1,1,0.1)) #create some data.frame test$z <- test$x/test$y #add a column ? HTH, Ivan Le 5/28/2010 13:52, Andre Easom a ?crit :> 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. > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
On Fri, May 28, 2010 at 12:52 PM, 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.Well, you can also access columns by number, so if you know what columns your x's and y's are in you can do: if you know the columns your x's and y's are in you can do it all at once: 3-column example: > foo=data.frame(x1=1:10,x2=1:10,x3=1:10,y1=10:1,y2=runif(10),y3=runif(10)) > foo[,1:3]/foo[,4:6] x1 x2 x3 1 0.1000000 2.037364 4.242166 2 0.2222222 38.651953 2.475068 3 0.3750000 9.351609 4.682223 etc you can then add this to your data frame: > foo=cbind(foo,foo[,1:3]/foo[,4:6]) > foo x1 x2 x3 y1 y2 y3 x1 x2 x3 1 1 1 1 10 0.49083037 0.2357286 0.1000000 2.037364 4.242166 2 2 2 2 9 0.05174383 0.8080586 0.2222222 38.651953 2.475068 3 3 3 3 8 0.32080042 0.6407213 0.3750000 9.351609 4.682223 etc and fix up the names: > names(foo)[7:9]=paste("r",1:3,sep="") > foo x1 x2 x3 y1 y2 y3 r1 r2 r3 1 1 1 1 10 0.49083037 0.2357286 0.1000000 2.037364 4.242166 2 2 2 2 9 0.05174383 0.8080586 0.2222222 38.651953 2.475068 3 3 3 3 8 0.32080042 0.6407213 0.3750000 9.351609 4.682223 Barry
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. >