R语言中如何将数据从矩阵转换为3列的形式

1、方式1

test <- matrix(1:12, byrow = T, nrow = 3, ncol = 4)
test
c1 <- rep(1:nrow(test), times = ncol(test))
c2 <- rep(1:ncol(test), each = nrow(test))
c3 <- unlist(as.data.frame(test))

result <- data.frame(c1, c2, c3)
result

2、方式2

test <- matrix(1:12, byrow = T, nrow = 3, ncol = 4)
test
c1 <- rep(1:nrow(test), each = ncol(test))
c2 <- rep(1:ncol(test), times = nrow(test))
c3 = unlist(as.data.frame(t(test)))
result <- data.frame(c1, c2, c3)
result

原文地址:https://www.cnblogs.com/liujiaxin2018/p/15505135.html