李昕垚的博客

reshape2包简介

excel里有一个数据透视表,常用的功能大概就是分组统计,R里的reshape2也能轻松实现数据透视表的功能。
reshape2包常用的很强大的两个函数是meltcast,通常叫做融合重铸

melt

melt的用法

1
2
melt(data, id.vars, measure.vars, variable.name = "variable", ..., na.rm = FALSE,
value.name = "value", factorsAsStrings = TRUE)

用帮助里的例子来说明

1
2
> names(ChickWeight) = tolower(names(ChickWeight))#Translate characters in character vectors
> head(ChickWeight)

ChickWeight数据集融合,以time,chick,diet作为ID

1
> chick_m = melt(ChickWeight, id = 2:4)

如有缺失值需删除可写为melt(ChickWeight,id=2:4,na.rm=TRUE)

cast

timevariable平均值

1
> dcast(chick_m, time ~ variable, mean)

计数

1
> dcast(chick_m, time ~ diet, length)

汇总

1
> dcast(chick_m, time ~ diet, length, margins = TRUE)

加限定,需加载plyr

1
> acast(chick_m, chick ~ time, mean, subset = .(time < 10 & chick < 20))
请李昕垚吃个糖?