Hello list, I'd like to use a variable (or a column of a data frame) by using its name as a string. E.g.: Data2003 <- c(150,200,120) Data2004 <- c(145,211,110) myvar1 <- "Data2003" myvar2 <- "Data2004" # now I'd like do do this total <- Data2003 + Data2004 # in any way like # total <- ???(myvar1, myvar2) # or # total <- ???(myvar1) + ???(myvar2) # or something like that Is there a possibility to do this in R - can't find a solution! Thanks a lot
look at ?get, this is also in R-FAQ 7.21 get(myvar1) + get(myvar2) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Oskar Villani" <o.villani at utanet.at> To: <r-help at stat.math.ethz.ch> Sent: Thursday, November 04, 2004 2:58 PM Subject: [R] calling a var by name in another var> Hello list, > > I'd like to use a variable (or a column of a data frame) by using > its name as a > string. E.g.: > > Data2003 <- c(150,200,120) > Data2004 <- c(145,211,110) > > myvar1 <- "Data2003" > myvar2 <- "Data2004" > > # now I'd like do do this > > total <- Data2003 + Data2004 > > # in any way like > # total <- ???(myvar1, myvar2) > # or > # total <- ???(myvar1) + ???(myvar2) > # or something like that > > Is there a possibility to do this in R - can't find a solution! > > Thanks a lot > > ______________________________________________ > 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 >
On Thu, 4 Nov 2004, Oskar Villani wrote:> Hello list, > > I'd like to use a variable (or a column of a data frame) by using its name as a > string. E.g.: > > Data2003 <- c(150,200,120) > Data2004 <- c(145,211,110) > > myvar1 <- "Data2003" > myvar2 <- "Data2004" > > # now I'd like do do this > > total <- Data2003 + Data2004 > > # in any way like > # total <- ???(myvar1, myvar2) > # or > # total <- ???(myvar1) + ???(myvar2) > # or something like that >A) This is a FAQ B) Many people who think they want to do this would be better off putting the variables into a list. -thomas
get(myvar1) + get(myvar2) [1] 295 411 230 On Thu, 2004-11-04 at 13:58, Oskar Villani wrote:> Hello list, > > I'd like to use a variable (or a column of a data frame) by using its name as a > string. E.g.: > > Data2003 <- c(150,200,120) > Data2004 <- c(145,211,110) > > myvar1 <- "Data2003" > myvar2 <- "Data2004" > > # now I'd like do do this > > total <- Data2003 + Data2004 > > # in any way like > # total <- ???(myvar1, myvar2) > # or > # total <- ???(myvar1) + ???(myvar2) > # or something like that > > Is there a possibility to do this in R - can't find a solution! > > Thanks a lot > > ______________________________________________ > 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 >