按组计算最小值和最大值(范围) [英] Calculate min and max (range) by group

查看:70
本文介绍了按组计算最小值和最大值(范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据框中有这样的东西:

I have something like this in a data frame:

PersonId Date_Withdrawal
       A      2012-05-01   
       A      2012-06-01
       B      2012-05-01
       C      2012-05-01
       A      2012-07-01
       A      2012-10-01
       B      2012-08-01
       B      2012-12-01
       C      2012-07-01

我想通过PersonId"获取最小和最大日期

I'd like to obtain the min and max date by 'PersonId'

推荐答案

首先,转换为适当的日期类(始终是一个好习惯),然后您可以按组运行一个简单的 range.这是一个尝试

First, convert to a proper date class (always a good practice) and then you could run a simple range by group. Here's an attempt

library(data.table)
setDT(df)[, Date_Withdrawal := as.IDate(Date_Withdrawal)]
df[, as.list(range(Date_Withdrawal)), by = PersonId]
#    PersonId         V1         V2
# 1:        A 2012-05-01 2012-10-01
# 2:        B 2012-05-01 2012-12-01
# 3:        C 2012-05-01 2012-07-01

library(dplyr)
df %>%
  mutate(Date_Withdrawal = as.Date(Date_Withdrawal)) %>%
  group_by(PersonId) %>%
  summarise(Min = min(Date_Withdrawal), Max = max(Date_Withdrawal))
# Source: local data frame [3 x 3]
# 
#  PersonId        Min        Max
#    (fctr)     (date)     (date)
# 1        A 2012-05-01 2012-10-01
# 2        B 2012-05-01 2012-12-01
# 3        C 2012-05-01 2012-07-01

<小时>

附言base aggregate 看起来像 aggregate(as.Date(Date_Withdrawal) ~ PersonId, df, range) 但它拒绝保留类.


P.S. base aggregate would look like aggregate(as.Date(Date_Withdrawal) ~ PersonId, df, range) but it refuses to retain classes .

这篇关于按组计算最小值和最大值(范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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