reshape包
melt函数
#数据重塑
#reshape包中的melt函数和cast函数
#构建数据
mydata=data.frame(id=c(1,1,2,2),time=c(1,2,1,2),x1=c(3,5,6,7),x2=c(2,4,6,7))
mydata
#加载reshape包
library(reshape)
#melt函数参数解释.id.vars表示融合字段保留的属性,measure.vars表示需要融合的字段
(melt(mydata))
(md=melt(mydata,id.vars = c("id","time")))
(md1=melt(mydata,id.vars = c("id","time"),measure.vars = "x1"))
cast函数
#cast进行数据重铸
cast(md,id~variable,mean)
cast(md,time~variable,mean)
cast(md,id+time~variable)
cast(md,id~variable+time)