列表中的警告消息和没有R的列表的罚款,为什么?

时间:2017-07-03 17:55:03

标签: r

假设我有这个矩阵:

View

然后我收到了这条警告信息:

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))
}

然后结果不被接受:

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

但是,如果没有列表,它可以正常工作!!

> fam
[[1]]
[1] 0

[[2]]
[1] 0

[[3]]
[1] 0

哪里是我的错误?

2 个答案:

答案 0 :(得分:1)

而不是fam[i]尝试fam[[i]]

答案 1 :(得分: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