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

 

posted @ 2021-11-03 18:18  小鲨鱼2018  阅读(1379)  评论(0编辑  收藏  举报