reshape2包中的melt()函数,数据融合,变成长型数据

library(reshape2)

x<-data.frame(id=1:6,

              name=c("wang","zhang","li","chen","zhao","song"),
              shuxue=c(89,85,68,79,96,53),
              yuwen=c(77,68,86,87,92,63))
#根据id和name两列,把后面的shuxue和yuwen两列合成一列。

x1<-melt(x,id=c("id","name"))

和melt()函数功能相反的是cast()函数,可以把长型数据转换成你想要的任何宽型数据。要输出结果为data.frame,则用dcast()函数。

x2<- dcast(x1,id~variable)

原文地址:https://www.cnblogs.com/liuyin-2019/p/11809646.html