HI Folks, I'm trying to create a list with named elements. Only, I don't know the names of the elements a priori (they come from the data being calculated). Currently, my approach is to create an environment, then assign things to the environement, then as.list the environment to get a list. Running the code gives, for example:> e2 <- new.env(FALSE, NULL) > assign("dude", 123, env=e2) > assign("chick", 456, env=e2) > as.list(e2)Error in as.vector(x, "list") : cannot coerce to vector Is this the best way to make a list like this? Thanks, Ben
Dear Ben, Is this the kind of thing you had in mind?> lst <- list() > element <- "a" > lst[[element]] <- 1:5 > element <- "b" > lst[[element]] <- letters[1:5] > lst$a [1] 1 2 3 4 5 $b [1] "a" "b" "c" "d" "e" I hope this helps, John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ben Shapiro > Sent: Friday, October 08, 2004 11:46 AM > To: r-help at stat.math.ethz.ch > Subject: [R] creating named elements of lists on the fly > > HI Folks, > > I'm trying to create a list with named elements. Only, I > don't know the names of the elements a priori (they come from > the data being calculated). Currently, my approach is to > create an environment, then assign things to the > environement, then as.list the environment to get a list. > > Running the code gives, for example: > > e2 <- new.env(FALSE, NULL) > > assign("dude", 123, env=e2) > > assign("chick", 456, env=e2) > > as.list(e2) > Error in as.vector(x, "list") : cannot coerce to vector > > Is this the best way to make a list like this? > > Thanks, > Ben
Ben Shapiro wrote:> HI Folks, > > I'm trying to create a list with named elements. Only, I don't know the names of the elements a priori (they come from the data being calculated). Currently, my approach is to create an environment, then assign things to the environement, then as.list the environment to get a list. > > Running the code gives, for example: > >>e2 <- new.env(FALSE, NULL) >>assign("dude", 123, env=e2) >>assign("chick", 456, env=e2) >>as.list(e2) > > Error in as.vector(x, "list") : cannot coerce to vector > > Is this the best way to make a list like this? > > Thanks, > Ben >You can use the "[[" operator for list objects: nm <- c("dude", "chick") e2[[nm[1]]] <- 123 e2[[nm[2]]] <- 456 HTH, --sundar
Ben Shapiro <bshapiro-lists-R <at> getdown.org> writes:
:
: HI Folks,
:
: I'm trying to create a list with named elements. Only, I don't know
the
names of the elements a priori (they
: come from the data being calculated). Currently, my approach is to create an
environment, then assign
: things to the environement, then as.list the environment to get a list.
:
: Running the code gives, for example:
: > e2 <- new.env(FALSE, NULL)
: > assign("dude", 123, env=e2)
: > assign("chick", 456, env=e2)
: > as.list(e2)
: Error in as.vector(x, "list") : cannot coerce to vector
:
: Is this the best way to make a list like this?
:
: Thanks,
: Ben
If you need to add them one at a time then John Fox has
already provided an answer. If you want to create it
all at once and you have the contents in a list and
the names in a vector like this:
nams <- letters[1:3]
contents <- list(1:3, 4:5, 6:9)
# then here are two ways:
# 1
mapply("{", nams, contents, SIMPLIFY = FALSE)
# 2
names(contents) <- nams
contents
# Actually in your example the elements are all homogeneous so
# you could alternately use a vector to hold the results:
# 1a
contents <- c(1, 2, 3)
mapply("{", nams, contents)
# 2a
names(contents) <- nams
contents