如何按组获取最小值 [英] 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?
更新:
我尝试过
Update:
I've tried to use summarize
before,
new.df <- test.df %>%
group_by(id) %>%
summarize(first.login = min(date))
,但它返回单个行和列。
but it returns a single row and column.
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
在内部工作
这篇关于如何按组获取最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!