Hi!
What about as.forumla()?
Like this:
form <- as.formula(paste("num", y, "~MemberID", sep=""))
agg<-aggregate(form, right.a, sum)
Would it work as you expect to?
HTH,
Ivan
Le 7/13/2011 19:30, Daniel Nordlund a écrit :
-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of UriB
Sent: Wednesday, July 13, 2011 9:28 AM
To: r-help@r-project.org
Subject: Re: [R] How to translate string to variable inside a command in
an easy way in R
Thanks
Here is another question
I want to have a function that get a string for example
y="AMI" and make commands like the following
agg<-aggregate(numAMI ~MemberID,right.a,sum)
Note that I know that numAMI is part of right.a because another function
with the string AMI already generated it.
Is there a way to do it without paste parse and eval?
I can do it by the following commands for y="AMI"
numy<-paste("num",y,sep="")
texta<-paste("agg<-aggregate(",numy,sep="")
text2<-"~MemberID, right.a, sum)"
text1<-paste(texta,text2,sep="")
eval(parse(text=text1))
If you can have a shorter code for it then it can be productive.
Well, you could do the above in one line (sorry, my email client is breaking
the line)
agg<- eval(parse(text=paste("aggregate(num",y,"~MemberID,right.a,sum)",sep='')))
You can put that in a function and call it like
your_function<- function(y)
eval(parse(text=paste("aggregate(num",y,"~MemberID,right.a,sum)",sep='')))
agg<- your_function(y='AMI')
However, you seem to be trying to wrestle R to the ground to get it to do
things your way. Maybe if you gave R-help some context about your overall task
(including why you need to construct these commands), someone could provide
suggestions on solving your programming tasks in a more R-ish way. Just a
thought.
Dan
Daniel Nordlund
Bothell, WA USA
______________________________________________
R-help@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
Dept. Mammalogy
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
______________________________________________
R-help@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.