R - 如何按这种方式列出的组值进行聚合

时间:2016-03-18 01:41:44

标签: r aggregate

给定DF,如何聚合它以列出具有连续匹配类型的行数?

例如,我想从DF生成out1或out2。

DF <- data.frame("Ages"=c(1,2,3,4,5,6,7,8),"Type"=c("R","N","N","N","R","R","N","R"))
DF
  Ages Type
1    1    R
2    2    N
3    3    N
4    4    N
5    5    R
6    6    R
7    7    N
8    8    R
out1 <- data.frame("Counts"=c(1,3,2,1,1))
out1
  Counts
1      1
2      3
3      2
4      1
5      1
out2 <- data.frame("Ages"=c(1,4,6,7,8),"Counts"=c(1,3,2,1,1))
out2
  Ages Counts
1    1      1
2    4      3
3    6      2
4    7      1
5    8      1

0 个答案:

没有答案