如何按组获得最小值 [英] How to get a minimum value by group
问题描述
我有一个像这样的数据框
I have a data frame looks like this
library(dplyr)
test.df <- data.frame(id=c(1,1,1,3,3,3,3),
date=c("2016-02-13","2016-06-01",
"2016-09-01","2015-08-02",
"2015-09-21","2016-12-01",
"2017-02-11"))
test.df$date <- as.Date(test.df$date,format='%Y-%m-%d')
id date
1 2016-02-13
1 2016-06-01
1 2016-09-01
3 2015-08-02
3 2015-09-21
3 2016-12-01
3 2017-02-11
我想创建一个新变量 first.login
来获取每个 id
的第一个日期.输出将如下所示
And I want to create a new variable first.login
to get first date of each id
. The output will look like this
id date first.login
1 2016-02-13 2016-02-13
1 2016-06-01 2016-02-13
1 2016-09-01 2016-02-13
3 2015-08-02 2015-08-02
3 2015-09-21 2015-08-02
3 2016-12-01 2015-08-02
3 2017-02-11 2015-08-02
我尝试使用这样的代码
new.df <- test.df %>%
group_by(id) %>%
mutate(first.log = min(date))
但这给出的结果是提取整个数据框的最早日期,而不是在每个 ID 组中.
But this gives the result that extracts earliest date for the whole data frame, not within each ID group.
id date first.login
1 2016-02-13 2015-08-02
1 2016-06-01 2015-08-02
1 2016-09-01 2015-08-02
3 2015-08-02 2015-08-02
3 2015-09-21 2015-08-02
3 2016-12-01 2015-08-02
3 2017-02-11 2015-08-02
这不应该是一项棘手的任务,但我想知道我犯了什么错误?如何在每个 id
组中获得最早的?
This shouldn't be a tricky task, but I was wondering what mistake did I make? How can I get the earliest within each id
group?
更新:我之前尝试过使用 summarize
,
Update:
I've tried to use summarize
before,
new.df <- test.df %>%
group_by(id) %>%
summarize(first.login = min(date))
但它返回单行和单列.
first.log
2015-08-02
事实证明这些代码没有问题;我只需要在其中指定 dplyr::mutate
.
It turns that there's nothing wrong with these codes; I just need to specify dplyr::mutate
in it.
推荐答案
这是一个循序渐进的 R 基础解决方案:
Here's a step-by-step R base solution:
# renaming for easy handle
x <- test.df$date
g <- test.df$id
# getting min
split(x, g) <- lapply(split(x, g), min)
# merging
test.df$first.login <- do.call("c", split(x, g))
#printting result
test.df
id date first.login
1 1 2016-02-13 2016-02-13
2 1 2016-06-01 2016-02-13
3 1 2016-09-01 2016-02-13
4 3 2015-08-02 2015-08-02
5 3 2015-09-21 2015-08-02
6 3 2016-12-01 2015-08-02
7 3 2017-02-11 2015-08-02
实际上这就是 ave
在内部的工作方式
Actually this is how ave
Works inside
这篇关于如何按组获得最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!