我有以下数据并且我试图总结它,以便我有每个名字的id,音调和音调计数。但是,我也希望得到音调率(tone_cnt /该名称的id之和)。不幸的是,我已经弄乱了这个并且运气不好。
df = data.frame(name=c("a","a","a","a","a","b","b","b","b","b","b"),
id = c(35, 35, 35, 36, 36, 35, 35, 35, 36, 36, 36),
tone=c(0,1,1,2,2,3,2,1,2,2,2))
df
sum = ddply(df, .(name, id, tone), summarise,
Tone_Cnt=length(tone), Tone_Rate=Tone_Cnt/sum(Tone_Cnt))
sum
已经弄乱了length(),sum(),sum(length()),似乎没有任何东西可以完成。目前,它只为每个人吐出1个。
以下是它的外观:
> sum
name id tone Tone_Cnt Tone_Rate
1 a 35 0 1 1 / (1+2)
2 a 35 1 2 2 / (1+2)
3 a 36 2 2 2 / (2)
4 b 35 1 1 1 / (1+1+1)
5 b 35 2 1 1 / (1+1+1)
6 b 35 3 1 1 / (1+1+1)
7 b 36 2 3 3 / (3)
它基本上是音调数除以(名字,内容,音调)总数的总和