reshape2包中melt函數:變長數據
示例1:默認melt參數
自己做一個寬數據:
image.png
melt之后的長數據,就是豎著排
image.png
這是非常直觀,也非常標準的例子。因為只有name這一列是字符型charactor,其他三列是數值型numeric。使用melt直接融合時,在默認參數下,將name列作為分類id variables,其他的三列,每一列的列名展開,再跟數值。
1/安裝,加載reshape2包包
install.packages("reshape2")
library(reshape2)
?reshape2#不行看不了
help(package=reshape2)#查看說明得這樣看,難道是因為這是包中的一個函數,應該是
2/導入xlsx,賦值為report,用melt融合,融合后賦值
readWorkbook("D:/R/RData/report.xlsx")
report <- readWorkbook("D:/R/RData/report.xlsx")
melt(report)#Using name as id variables
report_melt <- melt(report)
3/用View查看一下,也是OK的
View(report)
View(report_melt)
4/保存輸出為csv,*****fileEncoding = "GBK"避免中文輸出亂碼,非常關鍵, row.names = FALSE保存后不會多加一列行號碼
write.csv(report_melt,file="D:/R/RData/report_melt3.csv",fileEncoding = "GBK")
write.csv(report_melt,file="D:/R/RData/report_melt4.csv",fileEncoding = "GBK",row.names = FALSE)
這篇帖子中有關于幾種格式的介紹https://blog.csdn.net/qq_37859539/article/details/79857476