Anthony Damico
2010-Feb-22 19:49 UTC
[R] Re-assigning variables stored as character strings in another variable
Is there any way to get the last line of this code to double the contents of
a and b without naming them directly?
#create variables a and b
a<-5
b<-10
#store variable names a and b in variables c and d
c<-"a"
d<-"b"
e<-c(c,d)
#loop through both variables
for (i in e){
#print the numbers five and ten using only the variables c and d
#this line works fine
print(eval(parse(text=i)))*2
#re-assign variables a and b using only variables c and d
#this line does not work -
parse(text=i) <- eval(parse(text=i))*2
}
[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Feb-22 19:58 UTC
[R] Re-assigning variables stored as character strings in another variable
See ?get and ?assign On Mon, Feb 22, 2010 at 4:49 PM, Anthony Damico <ajdamico at gmail.com> wrote:> Is there any way to get the last line of this code to double the contents of > a and b without naming them directly? > > #create variables a and b > a<-5 > b<-10 > > #store variable names a and b in variables c and d > c<-"a" > d<-"b" > > e<-c(c,d) > > #loop through both variables > for (i in e){ > > #print the numbers five and ten using only the variables c and d > #this line works fine > print(eval(parse(text=i)))*2 > > #re-assign variables a and b using only variables c and d > #this line does not work - > parse(text=i) <- eval(parse(text=i))*2 > } > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
William Dunlap
2010-Feb-22 19:59 UTC
[R] Re-assigning variables stored as character strings in anothervariable
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Anthony Damico > Sent: Monday, February 22, 2010 11:50 AM > To: r-help at r-project.org > Subject: [R] Re-assigning variables stored as character > strings in anothervariable > > Is there any way to get the last line of this code to double > the contents of > a and b without naming them directly? > > #create variables a and b > a<-5 > b<-10 > > #store variable names a and b in variables c and d > c<-"a" > d<-"b" > > e<-c(c,d) > > #loop through both variables > for (i in e){ > > #print the numbers five and ten using only the variables c and d > #this line works fine > print(eval(parse(text=i)))*2 > > #re-assign variables a and b using only variables c and d > #this line does not work - > parse(text=i) <- eval(parse(text=i))*2 > }Don't use eval(parse(text=...)) until you have run out of more reliable options. In most cases storing your variables in a list makes life easier. You don't have to maintain a separate dataset with the names of the variables of interest and you don't have to worry about variables with funny names that won't parse. Try replacing your code with: > vars <- list(a=7, b=13) > for(i in names(vars)) { + print(vars[[i]]) + vars[[i]] <- vars[[i]] * 2 + } [1] 7 [1] 13 > vars $a [1] 14 $b [1] 26 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >