Reputation: 10538
I have a dataframe:
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)
Where I need to pick and apply the minimum created date for each id
.
Output:
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
Example: ZX1
"created" should be 2012-09-07
for all observations.
Upvotes: 0
Views: 28
Reputation: 887158
Try
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
Or using data.table
library(data.table)
setDT(df)[, created1 := min(as.Date(created)), by=id][]
Upvotes: 1