1

OK, this really basic. How do I add an element to a (named) list in R?

EDIT when the key name is a varibale

for (name in names(list$filenames)) {
  filename <- list$filenames[[name]]
  x <- read.table(filename)
  ret$name <- x # I want name to be interpreted here, not use "name"
}
David B
  • 29,258
  • 50
  • 133
  • 186
  • possible duplicate of [R: How to add variable key/value pair to list object?](http://stackoverflow.com/questions/1105659/r-how-to-add-variable-key-value-pair-to-list-object) – hadley Aug 08 '10 at 14:01

2 Answers2

2

Perhaps ret<-lapply(list$filenames,read.table) will be better?

Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
0

Re:

ret$name <- x # I want name to be interpreted here, not use "name"

That's the same thing as this:

ret[["name"]]

Do this instead:

ret[name]

Or if you want a simple vector back rather than something as the same type as ret, do this:

ret[[name]]