[R] assign to an element of a vector

Liaw, Andy andy_liaw at merck.com
Thu Apr 28 03:41:09 CEST 2005


You did not explain the full context of what you are trying to do.  Perhaps
this could help:

> varName <- as.name("bahbah")
> varName
bahbah
> 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
> 
> 
>




More information about the R-help mailing list