给定矩阵
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
。替换功能在这里似乎也不起作用,返回相同的错误消息。任何的想法?