XUN ZHANG
XUN ZHANG

Reputation: 93

how to create matrix in a list() in R

The following are my R codes.I can have ever output bbb[[1]][[i]],but why I can't run the code like bbb[[2]][[1]]<-matrix(rep(1.5,10*15),10,15)???

bbb<-list(list())
for(i in 1:10)
{
  bbb[[1]][[i]]<-matrix(rep(i,10*15),10,15)
}

Upvotes: 1

Views: 32

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145775

When you do bbb <- list(list()), it is equivalent to bbb <- list(); bbb[[1]] <- list(). It makes bbb a list, and makes the first item of bbb a sublist. bbb has length 1. You can assign something to bbb[[2]], but it is putting a new thing there. When you do bbb[[2]][[1]] <- ... you are trying to use bbb[[2]] as if it is already a list, but only bbb[[1]] is already a list. Use bbb[[2]] <- list() first, and then you will be able to use bbb[[2]][[1]] <- ...

Upvotes: 3

Related Questions