2012-11-24 50 views
-2

我有一个数据集,看起来像这样由两个不同的列

year month age 
2007 1  17 
2007 1  18 
2007 1  19 
2007 1  30 
2007 1  31 
2007 2  18 
2007 2  19 
2007 2  30 
2008 2  41 
2008 2  52 
2008 2  49 
2008 3  23 
2008 3  19 
2008 3  39 

而且我坚持试图通过每年和每月找到四分位数组分组如何找到四分位数。

的结果应该是这样的:

2007 1 Q1 Q2 Q3 Q4 
2007 2 Q1 Q2 Q3 Q4 

等。

感谢

回答

3

你的问题有点混乱。它只需要三个切点分成四分位数。那么你在第一季度,第二季度,第三季度,第四季度的数据中究竟想要什么?如果你想要的话,它似乎有点无聊。我会假设你想得到最小25th.pctile,中位数75th.pctile和最大值:

do.call (rbind, with(dfrm, tapply(age, interaction(year=year , month=month), quantile, 
                  probs=c(0, .25,.5, 0.75, 1)))) 
#--------------------- 
     0% 25% 50% 75% 100% 
2007.1 17 18.0 19 30.0 31 
2007.2 18 18.5 19 24.5 30 
2008.2 41 45.0 49 50.5 52 
2008.3 19 21.0 23 31.0 39 
4

总结这一点。

> aggregate(.~year + month, data=age, FUN=fivenum) 
    year month age.1 age.2 age.3 age.4 age.5 
1 2007  1 17.0 18.0 19.0 30.0 31.0 
2 2007  2 18.0 18.5 19.0 24.5 30.0 
3 2008  2 41.0 45.0 49.0 50.5 52.0 
4 2008  3 19.0 21.0 23.0 31.0 39.0 


> dput(age) 
structure(list(year = c(2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L), month = c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), age = c(17L, 
18L, 19L, 30L, 31L, 18L, 19L, 30L, 41L, 52L, 49L, 23L, 19L, 39L 
)), .Names = c("year", "month", "age"), class = "data.frame", row.names = c(NA, 
-14L))