[R] assigning a list item using a variable for a name

From: RSVP <paul_at_ptrow.com>
Date: Tue, 22 Mar 2011 16:47:19 -0700 (PDT)


I have a list.

my.list <- list(Tom=c(1,2,3), Dick=c(4,5,6), Harry=c(7,8,9))

I assign one of the names of the list to a variable.

name <- "Harry"

I can access the value of the list using the variable as follows:

 eval(parse(text=paste("my.list$", name, sep="")))

[1] 7 8 9

But how do I change the value of my.list$Harry using the variable name? This doesn't work:

 eval(parse(text=paste("my.list$", name, sep=""))) <- c(1,4,7)

Error in eval(parse(text = paste("my.list$", name, sep = ""))) <- c(1, :   target of assignment expands to non-language object

The following only replaces the first element of my.list$Harry

 my.list[name] <- c(1,4,7)
Warning message:
In my.list[name] <- c(1, 4, 7) :
  number of items to replace is not a multiple of replacement length

> my.list[name]

$Harry
[1] 1

Any suggestions?

Thanks.

--
View this message in context: http://r.789695.n4.nabble.com/assigning-a-list-item-using-a-variable-for-a-name-tp3398084p3398084.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
Received on Wed 23 Mar 2011 - 02:07:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Mar 2011 - 02:40:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive