繁体   English   中英

r中的行分组,比较和计数

[英]grouping, comparing, and counting rows in r

我有一个数据框,如下所示:

     system    Id initial final
665       9 16001    6070  6071
683      10 16001    6100  6101
696      11 16001    6101  6113
712      10 16971    6150  6151
715      11 16971    6151  6163
4966      7  4118   10238 10242
5031      9  4118   10260 10278
5088     10  4118   10279 10304
5115     11  4118   10305 10317


structure(list(system = c(9L, 10L, 11L, 10L, 11L, 7L, 9L, 10L, 
11L), Id = c(16001L, 16001L, 16001L, 16971L, 16971L, 4118L, 4118L, 
4118L, 4118L), initial = c(6070, 6100, 6101, 6150, 6151, 10238, 
10260, 10279, 10305), final = c(6071, 6101, 6113, 6151, 6163, 
10242, 10278, 10304, 10317)), .Names = c("system", "Id", "initial", 
"final"), row.names = c(665L, 683L, 696L, 712L, 715L, 4966L, 
5031L, 5088L, 5115L), class = "data.frame")

我想获得一个具有下一个结构的新数据框架

     Id  system length initial final
1 16001 9,10,11      3    6070  6113
2 16971   10,11      2    6150  6163
3  4118       7      1   10238 10242
4  4118 9,10,11      3   10260 10317


structure(list(Id = c(16001L, 16971L, 4118L, 4118L), system =     structure(c(3L, 
1L, 2L, 3L), .Label = c("10,11", "7", "9,10,11"), class =     "factor"), 
    length = c(3L, 2L, 1L, 3L), initial = c(6070L, 6150L, 10238L, 
    10260L), final = c(6113, 6163, 10242, 10317)), .Names = c("Id", 
"system", "length", "initial", "final"), class = "data.frame",     row.names = c(NA, 
-4L))

分组由Id和“system”字段中的差异(行之间)等于1。 此外,我想得到不同的“系统”以及分组中涉及多少“系统”。 最后一列包含第一个“初始”和最后一个“最终”。

在r中可以做到这一点吗? 谢谢。

您可以使用data.table 将“data.frame”转换为“data.table”( setDT ),通过获取“system”( diff(system) )的相邻元素的差异来创建分组变量“ cumsum ”,使用逻辑向量cumsum ,使用“Id”和“indx”作为分组变量来获取统计数据。

library(data.table)
 setDT(df)[,list(system=toString(system), length=.N, initial=initial[1L],
  final=final[.N]), by=list(Id,indx=cumsum(c(TRUE, diff(system)!=1)))][,
   indx:=NULL][]

#      Id    system length initial final
#1: 16001 9, 10, 11      3    6070  6113
#2: 16971    10, 11      2    6150  6163
#3:  4118         7      1   10238 10242
#4:  4118 9, 10, 11      3   10260 10317

或基于有关使用@ jazzurro的评论first/last从功能dplyr

 library(dplyr)
 df %>% 
    group_by(indx=cumsum(c(TRUE, diff(system)!=1)), Id) %>% 
    summarise(system=toString(system), length=n(), 
    initial=first(initial), final=last(final))

没有data.table的解决方案,但是plyr

library(plyr)

func = function(subdf)
{
    bool = c(diff(subdf$system),1)==1
    ldply(split(subdf, bool), function(u){
        data.frame(system = paste(u$system, collapse=','),
                   Id     = unique(u$Id),
                   length = nrow(u),
                   initial= head(u,1)$initial,
                   final  = tail(u,1)$final)
    })
}


ldply(split(df, df$Id), func)

#    .id  system length    Id initial final
#1 FALSE       7      1  4118   10238 10242
#2  TRUE 9,10,11      3  4118   10260 10317
#3  TRUE 9,10,11      3 16001    6070  6113
#4  TRUE   10,11      2 16971    6150  6163

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM