[R] cant find "get"?
Thomas Lumley
thomas at biostat.washington.edu
Fri Sep 1 01:32:28 CEST 2000
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list