简体   繁体   中英

New column that tells when observation first occurred

I am trying to create a new column 'First Appearance that tells me when the observation first occurred using the tidyverse package.

For Example: If I have

Year Observation
2000 A
2000 B
2001 A
2001 C

I would like the following outcome.

Year Observation First Appearance
2000 A 2000
2000 B 2000
2001 A 2000
2001 C 2001
library(dplyr)

df %>% 
  group_by(Observation) %>% 
  mutate(FirstObservation = min(Year)) %>% 
  ungroup()

Output

   Year Observation FirstObservation
  <int> <chr>                  <int>
1  2000 A                       2000
2  2000 B                       2000
3  2001 A                       2000
4  2001 C                       2001

Data

df <- structure(list(Year = c(2000L, 2000L, 2001L, 2001L), Observation = c("A", 
"B", "A", "C")), class = "data.frame", row.names = c(NA, -4L))

I know it is asked about tidyverse , still, I include a variant using data.table

library(data.table)
setDT(df)
df[,FistAppearance:= min(Year), Observation]
df
       Year Observation FistAppearance
1: 2000           A           2000
2: 2000           B           2000
3: 2001           A           2000
4: 2001           C           2001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM