我有一个数据框:
zz <- "id created status snap
ZX1 2012-09-07 A 2013-01-01
ZX1 2012-09-07 B 2013-01-02
ZX1 2012-10-11 B 2013-01-03
ZX1 2012-12-03 B 2013-01-04
ZY2 2014-01-04 A 2013-01-01
ZY2 2014-01-04 A 2013-01-04
ZZ3 2014-08-06 A 2013-01-01
ZZ3 2014-05-06 B 2013-01-03
ZZ3 2014-07-15 C 2013-01-04"
df <- read.table(text=zz, header=T)
我需要为每个id
选择并应用最小创建日期。
输出:
id created status snap
ZX1 2012-09-07 A 2013-01-01
ZX1 2012-09-07 B 2013-01-02
ZX1 2012-09-07 B 2013-01-03
ZX1 2012-09-07 B 2013-01-04
ZY2 2014-01-04 A 2013-01-01
ZY2 2014-01-04 A 2013-01-04
ZZ3 2014-05-06 A 2013-01-01
ZZ3 2014-05-06 B 2013-01-03
ZZ3 2014-05-06 C 2013-01-04
示例:ZX1
&#34;已创建&#34;所有观察结果应为2012-09-07
。
答案 0 :(得分:1)
尝试
library(dplyr)
df %>%
group_by(id) %>%
mutate(created=min(as.Date(created)))
# id created status snap
#1 ZX1 2012-09-07 A 2013-01-01
#2 ZX1 2012-09-07 B 2013-01-02
#3 ZX1 2012-09-07 B 2013-01-03
#4 ZX1 2012-09-07 B 2013-01-04
#5 ZY2 2014-01-04 A 2013-01-01
#6 ZY2 2014-01-04 A 2013-01-04
#7 ZZ3 2014-05-06 A 2013-01-01
#8 ZZ3 2014-05-06 B 2013-01-03
#9 ZZ3 2014-05-06 C 2013-01-04
或使用data.table
library(data.table)
setDT(df)[, created1 := min(as.Date(created)), by=id][]