0

The code below does exactly what I need, but there has to be a better way to do this. I need to create a list of lists with titles so that I can subset like this: position.list[["a.com"]]

There has to be a way to do this more efficiently, as what I have below is pretty horrendous. I saw several similar topics, but none seemed to do exactly what I need... I apologize if this has been answered before.

website.vec <- c("a.com", "b.com", "c.com")
position.list <- vector(len = length(website.vec))
position.list <- setNames(as.list(position.list), website.vec)
for (i in 1:length(position.list)) {
  position.list[[i]] <- list()
}
1
  • ah the uppercase "R" threw me off... Commented Dec 16, 2013 at 0:39

1 Answer 1

2

Option 1:

setNames(replicate(length(website.vec), list(), simplify=FALSE), website.vec)

Option 2:

lapply(setNames(nm=website.vec), function(x) list())
Sign up to request clarification or add additional context in comments.

3 Comments

i tried one just like the lapply() method, but without setNames. Thanks!
just ran a couple tests. looks like the the lapply() method is a bit faster (not that it matters for me, as website.vec will likely be no more than 100 elements long...)
the names in the results of lapply are the names of lapply's main argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.