简体   繁体   中英

making list of lists in R

Why in R

e=list(a,b,c,d)

is different than:

e=list(a,b)
e=list(e,c)
e=list(e,d)

?

The second approach can be easily used in a for loop, but it produces different result, and I create 1 object each iteration, so cant use first approach, any hints ?

If you absolutely want to use this approach, you can do this:

# Make up some data
a <- 1:3; b <- 4:5; c <- 6:10; d <- 11:17

# Build up the lists
e0 <- list(a, b, c, d)
e <- list(a, b)
e <- c(e, list(c))
e <- c(e, list(d))

# Compare the two
identical(e0, e) # TRUE

In a real-life case, however, instead of using a loop, you probably would be better off using function from the *apply family, such as lapply() , which will return a list of outputs directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM