[R] problem with assign and get

Sarah Goslee sarah.goslee at gmail.com
Mon Feb 27 18:57:40 CET 2012


Hi,

On Mon, Feb 27, 2012 at 12:41 PM, Jannis <bt_jannis at yahoo.de> wrote:
> Dear list members,
>
>
> does anyone have an idea why the following construction does not work but
> gives the following error message:
>
> assign('test', array(1:10, dim=c(10,10)))
> dimnames(get('test')) <- list(1:10,1:10)
>
>
> Error in dimnames(get("test")) <- list(1:10, 1:10) :
>  target of assignment expands to non-language object

There's no object to assign dimnames to. Think about it
this way - what do you expect the dimnames of to be
changed? "test" has no dimnames; get("test") would
print the object named "test", but how could you change
the dimnames of a displayed object?

If you want to change the dimnames of the object named
"test", you first need to use get("test") to assign that object
to a new object, then change the dimnames, then assign
the changed object to the desired name.

myobject <- get("test")
dimnames(myobject) <- list(1:10, 1:10)
assign("test", myobject)

Note: I'm assuming your small reproducible example (thank
you!) is a surrogate for something more complex, so that
dimnames(test) <- list(1:10, 1:10) is not an acceptable
solution. But frequently there are better options than the
use of get/assign for solving particular problems.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list