If ID matches set all of those ID's to be the earliest date

I have group ID's in data. These group ID's can have different dates. SO i need to replace the date's for the matching ID's to the earliest of the ID's. EX:
group ID: 1, 2, 4, 4, 4,
Date: 3/2, 3/2, 3/2, 3/4, 3/6

I need the matching group ID's -- 4 -- to all be set to have a date of 3/2. I want to keep all of the other information pertaining to that group id line unique, but the dates need to be changed to all be 3/2. So ultimately, 3 entries of group id 4 all with a date of 3/2.

If your date variable is of class "Date", then it should be no problem.


df _new <- df %>%
# group by ID
group_by(ID) %>%
# create new date variable (the one you need), which is the earliest date per id. "Date" is here your original date variable.
mutate(date_new =min(Date)) 

I get an error when i try this:

STO_Final <- group_by(STO$"Group_ID") %>% mutate(STO$"Start_date" == min(STO$"Start_date"))

Error in UseMethod("group_by") :
no applicable method for 'group_by' applied to an object of class "characte

Is STO the name of your data frame?
and Group_ID the name of your ID variable?
and Start_date the name of your date variable?
Then try this:

library(dplyr)
STO_Final <- STO %>% 
group_by(Group_ID) %>% 
mutate(Start_date = min(Start_date))
```

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.