I am trying to find a way to assign values to elements of a vector that will be defined by a user. So I don't have the name of the vector and cannot hard code the assignment in advance. In the example below I have to get() the vector using its name. When I try to assign to an element I get an error:> a <- c(1,2,3) > get('a')[1] <- 0Error: Target of assignment expands to non-language object Any suggestions? FS
how about
assign( 'a', { z <- get('a'); z[1] <- 0; z } )
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Fernando Saldanha
Sent: Wednesday, April 27, 2005 3:22 PM
To: Submissions to R help
Subject: [R] assign to an element of a vector
I am trying to find a way to assign values to elements of a vector that will
be defined by a user. So I don't have the name of the vector and cannot hard
code the assignment in advance. In the example below I have to get() the
vector using its name. When I try to assign to an element I get an error:
> a <- c(1,2,3)
> get('a')[1] <- 0
Error: Target of assignment expands to non-language object
Any suggestions?
FS
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
You did not explain the full context of what you are trying to do. Perhaps this could help:> varName <- as.name("bahbah") > varNamebahbah> substitute(a[1] <- 0, list(a=varName))bahbah[1] <- 0 So you could perhaps eval() this expression. Andy> From: Fernando Saldanha > > I am trying to find a way to assign values to elements of a vector > that will be defined by a user. So I don't have the name of the vector > and cannot hard code the assignment in advance. In the example below I > have to get() the vector using its name. When I try to assign to an > element I get an error: > > > a <- c(1,2,3) > > get('a')[1] <- 0 > Error: Target of assignment expands to non-language object > > Any suggestions? > > FS > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > >
Fernando Saldanha [fsaldan1 at gmail.com] wrote:> I am trying to find a way to assign values to elements of a vector > that will be defined by a user. > > a <- c(1,2,3) > > get('a')[1] <- 0 > Error: Target of assignment expands to non-language objectTry this function: g.assign <- function(i, pos=1, ..., value) { x <- if (pos > 0) get(i, pos) else get(i, , parent.frame()) x[...] <- value if (pos > 0) assign(i, x, pos) else assign(i, x, , parent.frame()) } Your example becomes: R> a <- c(1,2,3) R> g.assign("a", pos=1, 1, value=0) Note you use a positive <pos> (usually pos=1) for "global" variables, and pos=0 for "local" variables inside a function. The <...> construction allows this function to work for arrays too, but the downside is that you must type "value=" explicitly. -- David Brahm (brahm at alum.mit.edu)