reshape2包中melt函數(shù):變長(zhǎng)數(shù)據(jù)
示例1:默認(rèn)melt參數(shù)
自己做一個(gè)寬數(shù)據(jù):
melt之后的長(zhǎng)數(shù)據(jù)汉形,就是豎著排
這是非常直觀候学,也非常標(biāo)準(zhǔn)的例子驯镊。因?yàn)橹挥衝ame這一列是字符型charactor锅锨,其他三列是數(shù)值型numeric。使用melt直接融合時(shí)妆绞,在默認(rèn)參數(shù)下顺呕,將name列作為分類id variables,其他的三列括饶,每一列的列名展開株茶,再跟數(shù)值。
1/安裝图焰,加載reshape2包包
install.packages("reshape2")
library(reshape2)
?reshape2#不行看不了
help(package=reshape2)#查看說明得這樣看启盛,難道是因?yàn)檫@是包中的一個(gè)函數(shù),應(yīng)該是
2/導(dǎo)入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"避免中文輸出亂碼,非常關(guān)鍵拙绊, row.names = FALSE保存后不會(huì)多加一列行號(hào)碼
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)
這篇帖子中有關(guān)于幾種格式的介紹https://blog.csdn.net/qq_37859539/article/details/79857476