The index operator $ indexes lists by name. If you want to get the first item from an unnamed list of a , you need a[[1]] .
You can create a function that automatically adds names if they are not specified, similar to how data.frame works (this version is all or nothing), if some arguments are specified, it will not indicate the remaining, unnamed).
nlist <- function(...) { L <- list(...) if (!is.null(names(L))) return(L) n <- lapply(match.call(),deparse)[-1] setNames(L,n) } b <- c <- d <- 1 nlist(b,c,d) nlist(d=b,b=c,c=d)
For your second question, the answer is yes; did you try this ???
L <- list(a=1,b=2,c=list(d=5,e=9)) str(c(L)) ## List of 3 ## $ a: num 1 ## $ b: num 2 ## $ c:List of 2 ## ..$ d: num 5 ## ..$ e: num 9 str(c(L,recursive=TRUE)) ## Named num [1:4] 1 2 5 9 ## - attr(*, "names")= chr [1:4] "a" "b" "cd" "ce"
The first is a list that includes two numerical values ββand a list, the second has been flattened into a named numerical vector.
source share