0

I have a column in my df such as the EVENTDATE above which contains dates repeated and I want keep them as they are but create a new column as ADMISSION and only keep one of the value from EVENTDATE in it as shown above. How can I achieve this using R?

structure(list(ID = c(1, 1, 1, 2, 2), EVENTDATE = structure(c(18294, 
18294, 18294, 18322, 18322), class = "Date"), ADMISSION = structure(c(18294, 
NA, NA, 18322, NA), class = "Date")), row.names = c(NA, 5L), class = "data.frame")
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
kam
  • 345
  • 1
  • 8
  • Please share a reproducible piece of your data with `dput(head(data))` so that we could use it to help you more efficiently. – Anoushiravan R May 05 '21 at 08:08
  • Can you see the screenshot now? – kam May 05 '21 at 08:16
  • 2
    In order for us to help you, please provide a [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example. For example, to produce a minimal data set, you can use `head()`, `subset()`. Then use `dput()` to give us something that can be put in R immediately. Alternatively, you can use base R datasets (to see complete list `library(help = "datasets")`). Sceenshots are very bad because we can't use the data inside it (like copy/paste). – Paul May 05 '21 at 08:23
  • More general information abour repex here: https://stackoverflow.com/help/minimal-reproducible-example – Paul May 05 '21 at 08:26

2 Answers2

0

I think this solution might be able to help you:

library(dplyr)

df2 %>%
  group_by(id) %>%
  mutate(eventdate = as.Date(eventdate, "%d/%m/%Y"), 
         ID = row_number(),
         admission = ifelse(ID == 1, as.character(eventdate), NA)) 


# A tibble: 4 x 4
# Groups:   id [2]
     id eventdate     ID admission 
  <dbl> <date>     <int> <chr>     
1     1 2020-02-02     1 2020-02-02
2     1 2020-02-02     2 NA        
3     2 2020-03-01     1 2020-03-01
4     2 2020-03-01     2 NA  

Data

df2 <- tibble::tribble(
  ~ id, ~ eventdate, 
  1, "02/02/2020", 
  1, "02/02/2020",
  2, "01/03/2020",
  2, "01/03/2020"
)
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
0

You can try the base R code below, using ave + duplicated

transform(
  df,
  ADMISSION = ave(EVENTDATE, ID, FUN = function(x) replace(x, duplicated(x), NA))
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81