1

Suppose I have this matrix:

Matrix <- c(5, 2, 3, 1, 4,
                 0, 2, 3, 4, 1,
                 0, 0, 3, 4, 1,
                 0, 0, 0, 4, 1,
                 0, 0, 0, 0, 1)
      Matrix <- matrix(Matrix, 5, 5)
fam <- list()
for (i in 1:3){
fam[i] <- array(0, dim = dim(Matrix))
}

Then I got this warning messages:

Warning messages:
1: In fam[i] <- array(0, dim = dim(Matrix)) :
  number of items to replace is not a multiple of replacement length
2: In fam[i] <- array(0, dim = dim(Matrix)) :
  number of items to replace is not a multiple of replacement length
3: In fam[i] <- array(0, dim = dim(Matrix)) :
  number of items to replace is not a multiple of replacement length

Then the result is not as accepted:

> fam
[[1]]
[1] 0

[[2]]
[1] 0

[[3]]
[1] 0

However, without list it is work fine!!

> fam
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0

Where it is my mistake??

2 Answers2

1

Instead of fam[i] try fam[[i]].

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0
[[ selects a single element of a list

for (i in 1:3){
  fam[[i]] <- array(0, dim = dim(Matrix))
}

http://stackoverflow.com/questions/1169456/the-difference-between-and-notations-for-accessing-the-elements-of-a-lis

user_123
  • 62
  • 12