How to make a list of integer vectors in R

前端 未结 2 663
有刺的猬
有刺的猬 2021-02-02 07:56

Basic question: In R, how can I make a list and later populate it with vector elements?

l <- list() 
l[1] <- c(1,2,3) 

This gives the err

2条回答
  •  滥情空心
    2021-02-02 08:15

    Use [[1]] as in

    l[[1]] <- c(1,2,3)
    l[[2]] <- 1:4
    

    and so. Also recall that preallocation is much more efficient, so if you know how long your list is going to be, use something like

    l <- vector(mode="list", length=N)
    

提交回复
热议问题