2

给定矩阵

test <- structure(list(X1 = c(3L, 0L, 3L, 1L, 2L, 2L, 1L, 2L, 2L, 3L), 
X2 = c(2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), X3 = c(0L, 
0L, 3L, 0L, 2L, 2L, 3L, 0L, 0L, 2L), X4 = c(1L, 1L, 1L, 0L, 
3L, 1L, 3L, 1L, 1L, 1L), X5 = c(3L, 3L, 1L, 3L, 1L, 3L, 2L, 
3L, 3L, 3L), X6 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L
), X7 = c(2L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 2L), X8 = c(3L, 
0L, 1L, 0L, 1L, 1L, 3L, 0L, 0L, 1L), X9 = c(3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("X1", "X2", "X3", "X4", 
"X5", "X6", "X7", "X8", "X9"), row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10"), class = "data.frame")

我试图用数字序列 c(0,0,1) 替换“1”的每个实例,用 c(0,1,0) 替换 2,用 (1,0,1) 替换 3,用 (1,0,1) 替换 0 (0,0,0)。矩阵中的每个值都应替换为这些三个二进制值序列之一。结果nrow矩阵的 应该是nrow(test) * 3。显然我尝试使用索引test[test == 1] <- c(0,0,1),但这会返回错误rhs is the wrong length for indexing by a logical matrix。替换功能在这里似乎也不起作用,返回相同的错误消息。任何的想法?

4

4 回答 4

5

由于结果的维度不同(一个 3 维数组而不是一个 2 维矩阵),您不能只一个一个地替换元素。

您可以使用apply:if 经常用于对矩阵的每一列或每一行应用一个函数,但我们也可以使用它对每个元素应用一个函数。

apply( 
  test, 
  1:2, 
  function(u) list(c(0,0,0), c(0,0,1), c(0,1,0), c(1,0,1))[[u+1]] 
)
于 2012-05-29T00:43:29.110 回答
2

你可以这样做:

Z <- matrix(c(0, 1, 0, 0,
              0, 0, 1, 0,
              0, 0, 0, 1), nrow = 3)

sapply(test, function(i)Z[,i+1])
#      X1 X2 X3 X4 X5 X6 X7 X8 X9
# [1,]  0  1  0  0  0  0  1  0  0
# [2,]  0  0  1  0  0  0  0  0  0
# [3,]  1  0  0  0  1  1  0  1  1
# ...
# [28,]  0  1  1  0  0  0  1  0  0
# [29,]  0  0  0  0  0  0  0  0  0
# [30,]  1  0  0  0  1  1  0  0  1
于 2012-05-29T00:51:16.173 回答
2

您可以使用 switch 扩展:

matrix( sapply( c(1+data.matrix(test)), 
                    switch, c(0,0,0), c(0,0,1), c(0,1,0), c(1,0,1)) , 
        nrow=nrow(test)*3 ) 
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    1    0    0    0    1    1    0    1    1
 [2,]    0    1    0    0    0    0    1    0    0
 [3,]    1    0    0    1    1    1    0    1    1
 [4,]    0    0    0    0    1    1    0    0    1
 [5,]    0    0    0    0    0    0    1    0    0
 [6,]    0    1    0    1    1    1    0    0    1
 snipped remaining rows


str( matrix( sapply( c(1+data.matrix(test)), 
                    switch, c(0,0,0), c(0,0,1), c(0,1,0), c(1,0,1)) , 
        nrow=nrow(test)*3 ) )
  num [1:30, 1:9] 1 0 1 0 0 0 1 0 1 0 ...
于 2012-05-29T00:53:17.880 回答
1

您的输入是一个数据框,因此双重应用方法也可以工作:

f <- function(u) list(c(0,0,0), c(0,0,1), c(0,1,0), c(1,0,1))[[u+1]] 
#  borrowed from Vincent Zoonekynd's answer
sapply(test, sapply, f)

请注意,输出完全符合 OP 中的要求(“结果矩阵的 nrow 应为 nrow(test) * 3”)——这可能优于也可能不优于三维数组(的结果apply(1:2, ...)),具体取决于你想达到什么。

于 2018-10-03T10:30:11.777 回答