-1

enter image description here

I need to extract the earliest date of surgery from my file, but if I use the codes I'm used to, it deletes duplicated dates.

For example if a person has 2 different surgeries but at the same date, R deletes one of these surgeries, so I am left with only one of them.

I need to find a solution that can retrieve the earliest date of surgeries, but if one person have two different surgeries at the same time, I would like to keep both dates (which are then duplicates).

"D_ODTO" is the surgery date and "C_OPR" is the surgery type. "PNR" is ID names.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • 2
    Images are not the right way to share data/code. Add them in a reproducible format which is easier to copy. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Apr 02 '21 at 10:29

1 Answers1

0

If I understand your problem, you should be able to do this using functions from the tidyverse package. Suppose your data is in a data.frame called d:

library(tidyverse)

d %>%
  group_by(PNR, C_OPR) %>% #group by ID and surgery type
  slice_min(D_ODTO) # find the "min" (i.e. earliest) date

As Ronak said, without having a reproducible example I cannot test this code.

bouncyball
  • 10,631
  • 19
  • 31