开发者

Setting vector as a list component in R

I want to create a li开发者_开发技巧st which has 1 element called 'a' that holds a vector of doubles.

l<-list('a'=1:1000)

does the trick. However, what if I want to do it dynamically?

l<-list()
l['a']<-1:1000

does not work! How can I allocate enough memory when creating the list? Thanks


Then you do

> l<-list()
> l[['a']]<-1:10
> l
$a
 [1]  1  2  3  4  5  6  7  8  9 10

which works fine. With lists, [...] gives you a list with the selected elements, where [[...]] gives you the selected element. See also the help page ?Extract

EDIT : or, as Tim said, l$a <- 1:10 does the same. The advantage of [[...]] lies in

> l <- list()
> aname <- 'a'
> l[[aname]] <- 1:10
> l
$a
 [1]  1  2  3  4  5  6  7  8  9 10
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜