如何按照组合获取具有最早时间戳的数据帧? [英] How to get rows, by group, of data frame with earliest timestamp?

查看:112
本文介绍了如何按照组合获取具有最早时间戳的数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df <- data.frame(group=c(1,2,4,2,1,4,2,3,3),
             ts=c("2014-02-13","2014-06-01","2014-02-14","2014-02-11","2013-02-01","2014-02-02","2014-03-21","2014-12-01","2014-02-11"),
             letter=letters[1:9])
df$ts <- as.Date(df$ts,format='%Y-%m-%d')

我想找到一个操作,将生成包含每个组的最小时间戳的完整行,在这种情况下,

I want to find an operation that will produce the complete rows containing the minimum timestamp per group, in this case,

group         ts letter
    1 2013-02-01      e
    4 2014-02-02      f
    2 2014-02-11      d
    3 2014-02-11      i

一个快速而肮脏(和慢)的基本R解决方案将是

A quick and dirty (and slow) base R solution would be

dfo <- data.frame(df[order(df$ts,decreasing=F),],index=seq(1:nrow(df)))
mins <- tapply(dfo$index,dfo$group,min)
dfo[dfo$index %in% mins,]

直观地,我想如果有办法按组添加订单索引,那么我可以过滤到该列的值为1的位置,但是我不知道如何执行它,而不需要大量的子集和重新加入。

Intuitively, I think if there was a way to add an order index by group then I could just filter to where that column's value is 1, but I'm not sure how to execute it without lots of subsetting and rejoining.

推荐答案

您可以使用 dplyr

library(dplyr)
group_by(df, group) %>% summarise(min = min(ts), letter = letter[which.min(ts)]) 
#   group        min letter
# 1     1 2013-02-01      e
# 2     2 2014-02-11      d
# 3     3 2014-02-11      i
# 4     4 2014-02-02      f

你也可以切片排名行

group_by(df, group) %>% 
    mutate(rank = row_number(ts)) %>% 
    arrange(rank) %>%
    slice(1)

这篇关于如何按照组合获取具有最早时间戳的数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆