howdy gurus, I was wondering if someone could help me with what looks like a simple problem. I found this great function called "get" which allows me to work out my object name at run time. Unfortunately it's not letting me assign values with it. What am I doing wrong? thanks, John Strumila> names(get(file.name))[1][1] "X14.59.23"> names(get(file.name))[1] <- "date"Error: couldn't find function "get<-" -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 1 Sep 2000, Strumila, John wrote:> howdy gurus, > > I was wondering if someone could help me with what looks like a simple > problem. > > I found this great function called "get" which allows me to work out my > object name at run time. Unfortunately it's not letting me assign values > with it. What am I doing wrong? > > thanks, > John Strumila > > > names(get(file.name))[1] > [1] "X14.59.23" > > names(get(file.name))[1] <- "date" > Error: couldn't find function "get<-"When you type names(fred)[1]<-"barney" it looks like a normal assignment, but complicated things are happening under the hood. R actually goes and calls the functions "names<-" and "[<-" to handle what look like function calls on the left hand side. When you try to evaluate somefunction(x)<-y R goes to look for a function "somefunction<-" that says how to set the values of "somefunction" In your case, R is looking for a function "get<-" that would somehow change the value to be returned by get(file.name) in the future. You can't do that. In any case get(file.name) doesn't return the object whose name is in file.name, it returns a copy of that object. Changing the copy would get you nowhere. I could tell you how to create a reference to the object, rather than to a copy of the object, and manipulate that, but that is well known to cause seven years bad luck. What you want to do is eval(substitute(names(x)[1]<-"date",list(x=as.name(file.name)))) except that you would probably be better off just finding out what your variable was called and using its name. This sort of double indirection is just too complicated. -the person who is answering this email My full name and affiliation (for information only) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
thanks heaps Thomas, I played with substitute, eval and deparse but it would have taken me a month of sundays to figure this one out. I'm still trying to understand your explanation! I reckon this R-list and it's contributors are great. thanks, John Strumila -----Original Message----- From: Thomas Lumley [mailto:thomas at biostat.washington.edu] Sent: Friday, 1 September 2000 9:32 To: Strumila, John Cc: r-help at stat.math.ethz.ch Subject: Re: [R] cant find "get"? On Fri, 1 Sep 2000, Strumila, John wrote:> howdy gurus, > > I was wondering if someone could help me with what looks like a simple > problem. > > I found this great function called "get" which allows me to work out my > object name at run time. Unfortunately it's not letting me assign values > with it. What am I doing wrong? > > thanks, > John Strumila > > > names(get(file.name))[1] > [1] "X14.59.23" > > names(get(file.name))[1] <- "date" > Error: couldn't find function "get<-"... What you want to do is eval(substitute(names(x)[1]<-"date",list(x=as.name(file.name)))) except that you would probably be better off just finding out what your variable was called and using its name. This sort of double indirection is just too complicated. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>>>>> "JohnS" == Strumila, John <John.Strumila at team.telstra.com> writes:JohnS> I found this great function called "get" which allows me to work JohnS> out my object name at run time. Unfortunately it's not letting JohnS> me assign values with it. What am I doing wrong? >> names(get(file.name))[1] [1] "X14.59.23" >> names(get(file.name))[1] <- "date" Error: couldn't find function "get<-" in spite of Thomas' excellent explanation about `` foo(x) <- .. '' I wonder if John does not just need to be told `` ?assign '' E.g. myvar <- "X14.59.23" assign(myvar, list("some text", date = date())) get(myvar) ## i.e. X14.49.23 gives [[1]] [1] "some text" $date [1] "Fri Sep 1 08:29:45 2000" Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO D10 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._