Find lowest date in each group [duplicate]

匆匆过客 提交于 2019-12-12 02:19:15

问题


Hi there: I'm trying to find the lowest date in each group. The purpose is to find what date is common to each of several time series. Currently the data look like this.

library(tidyr)
library(dplyr)
grouping_variable<-sample(c('a', 'b', 'c'), 500, replace=TRUE)
date<-sample(seq(as.Date('1999/01/01'), as.Date('2015/01/01'), by="day"), 500)
numeric_variable<-rnorm(500, 50, sd=2)
df<-data.frame(grouping_variable, date, numeric_variable)

And my working attempt is basically this.

df %>%
group_by(grouping_variable)%>%
min(date)

回答1:


We can use slice

 df %>% 
    group_by(grouping_variable) %>% 
    slice(which.min(date))


来源:https://stackoverflow.com/questions/40224530/find-lowest-date-in-each-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!