我在数组的索引上复制矩阵时遇到了麻烦.下面是我问题的简化版本.我不明白R在这里做什么...
test_array <- array(NA, c(4,3,2))
test_matrix <- as.matrix(data.frame(rep(1, 4),rep(2, 4)))
test_array[,1:3,] <- test_matrix # I want this matrix duplicated across this array such that the below commands print the exact same result: the test_matrix
test_array[,1,]
test_array[,2,] # What's going on here?
test_array[,3,]
我错过了什么?如果上面的方法不起作用,那么为什么下面的方法工作得很好?
test_array <- array(NA, c(4,3,3))
test_matrix <- as.matrix(data.frame(rep(1, 4),rep(2, 4),rep(3, 4)))
test_array[,,1:3] <- test_matrix
test_array[,,1]
test_array[,,2]
test_array[,,3]
这也很好用,但我真的不想循环它:
test_array <- array(NA, c(4,3,2))
test_matrix <- as.matrix(data.frame(rep(1, 4),rep(2, 4)))
test_array[,1,] <- test_matrix
test_array[,2,] <- test_matrix
test_array[,3,] <- test_matrix
test_array[,1,]
test_array[,2,]
test_array[,3,]
干杯