rs-plus

How to create a list with names but no entries in R/Splus?


I'd like to set up a list with named entries whose values are left uninitialized (I plan to add stuff to them later). How do people generally do this? I've done:

mylist.names <- c("a", "b", "c")
mylist <- as.list(rep(NA, length(mylist.names)))
names(mylist) <- mylist.names

but this seems kind of hacky. There has to be a more standard way of doing this...right?


Solution

  • I would do it like this:

    mylist.names <- c("a", "b", "c")
    mylist <- vector("list", length(mylist.names))
    names(mylist) <- mylist.names