John Edwards
2011-Feb-15 16:43 UTC
[R] Error when modifying names of the object returned by get()
I get the following error when I try to modify the names of the object
returned by get(). Does anybody know how to do so? (I could use as.vector()
to remove the names, but I'm interested in how to modify the object returned
by get().)
$ cat main_get_name_assign.R
x=1:10
names(x)=letters[1:10]
names(get('x'))=LETTERS[1:10]
#names(x)=LETTERS[1:10]
x
$ Rscript main_get_name_assign.R> x=1:10
> names(x)=letters[1:10]
>
> names(get('x'))=LETTERS[1:10]
Error in names(get("x")) = LETTERS[1:10] :
target of assignment expands to non-language object
Execution halted
[[alternative HTML version deleted]]
Ista Zahn
2011-Feb-15 17:13 UTC
[R] Error when modifying names of the object returned by get()
Hi John,
Probably there is a more elegent solution, but I usually do something like
tmp <- get("x")
names(tmp) <- letters[1:10]
assign("x", tmp)
rm("tmp")
HTH,
Ista
On Tue, Feb 15, 2011 at 11:43 AM, John Edwards <jhnedwards603 at
gmail.com> wrote:> I get the following error when I try to modify the names of the object
> returned by get(). Does anybody know how to do so? (I could use as.vector()
> to remove the names, but I'm interested in how to modify the object
returned
> by get().)
>
> $ cat main_get_name_assign.R
> x=1:10
> names(x)=letters[1:10]
>
> names(get('x'))=LETTERS[1:10]
> #names(x)=LETTERS[1:10]
> x
>
> $ Rscript main_get_name_assign.R
>> x=1:10
>> names(x)=letters[1:10]
>>
>> names(get('x'))=LETTERS[1:10]
> Error in names(get("x")) = LETTERS[1:10] :
> ?target of assignment expands to non-language object
> Execution halted
>
> ? ? ? ?[[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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
Duncan Murdoch
2011-Feb-15 18:45 UTC
[R] Error when modifying names of the object returned by get()
On 15/02/2011 11:43 AM, John Edwards wrote:> I get the following error when I try to modify the names of the object > returned by get(). Does anybody know how to do so? (I could use as.vector() > to remove the names, but I'm interested in how to modify the object returned > by get().)You can probably do this by constructing a call to the `names<-` replacement function, but it's really bad style. Don't write R code that has external side effects if you can avoid it. In this case, you'll almost certainly get more maintainable code by writing your function to return a copy of x with new names, rather than trying to modify the original. Duncan Murdoch> $ cat main_get_name_assign.R > x=1:10 > names(x)=letters[1:10] > > names(get('x'))=LETTERS[1:10] > #names(x)=LETTERS[1:10] > x > > $ Rscript main_get_name_assign.R > > x=1:10 > > names(x)=letters[1:10] > > > > names(get('x'))=LETTERS[1:10] > Error in names(get("x")) = LETTERS[1:10] : > target of assignment expands to non-language object > Execution halted > > [[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.