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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM