Is there an R function to change a single date to a different date?

Viewed 26

I am new to R so bear with me. I noticed that one of the dates is wrong in my dataset that I would like to change. Below is a small subset of data from the dataset as an example.

admission2 (dttm) time_lorazepam2 (dttm) admission_to_lorazepam (dbl)
2015-08-05 01:52:00 2015-08-05 02:57:00 65
2015-08-17 02:00:00 2015-08-17 01:26:00 -34
2016-02-25 18:59:00 2016-02-25 20:15:00 76

The second row has a negative value of admission_to_lorazepam, which = time_length(difftime(se$time_lorazepam2, se$admission2), "minutes", indicating that the admission time is incorrect. I would like to change admission2 from '2015-08-17 02:00:00' to '2015-08-17 01:26:00' for that specific instance.

I have tried the mutate function with something like > se %>% + mutate(admission2=ifelse(admission2 = 2018-08-17 02:00:00, as.Date(admission2), 2018-08-17 01:26:00)) without any luck. This data set is large and I may need to change other specific dates when I notice an error.

Please let me know if I have any options.

1 Answers

Given that this is your data (here admission2 and time_lorazepam2 are already in datetime format):

library(dplyr)
library(tibble)

dat <- tribble(~ admission2, ~ time_lorazepam2, ~admission_to_lorazepam,
               "2015-08-05 01:52:00",   "2015-08-05 02:57:00", 65,
               "2015-08-17 02:00:00",   "2015-08-17 01:26:00",  -34,
               "2016-02-25 18:59:00",   "2016-02-25 20:15:00",  76
) %>% mutate(across(c(admission2, time_lorazepam2), as.POSIXct))

dat
#> # A tibble: 3 x 3
#>   admission2          time_lorazepam2     admission_to_lorazepam
#>   <dttm>              <dttm>                               <dbl>
#> 1 2015-08-05 01:52:00 2015-08-05 02:57:00                     65
#> 2 2015-08-17 02:00:00 2015-08-17 01:26:00                    -34
#> 3 2016-02-25 18:59:00 2016-02-25 20:15:00                     76

I think you want to do:

dat %>% mutate(admission2 = if_else(admission2 == as.POSIXct("2015-08-17 02:00:00"),
                           as.POSIXct("2015-08-17 01:26:00"),
                           admission2))

#> # A tibble: 3 x 3
#>   admission2          time_lorazepam2     admission_to_lorazepam
#>   <dttm>              <dttm>                               <dbl>
#> 1 2015-08-05 01:52:00 2015-08-05 02:57:00                     65
#> 2 2015-08-17 01:26:00 2015-08-17 01:26:00                    -34
#> 3 2016-02-25 18:59:00 2016-02-25 20:15:00                     76

To be save, you could add a condition admission_to_lorazepam < 0 , to really only change the line where admission is "2015-08-17 02:00:00" and admission_to_lorazepam is a negative number:

dat %>% mutate(admission2 = if_else(admission2 == as.POSIXct("2015-08-17 02:00:00") & admission_to_lorazepam < 0,
                           as.POSIXct("2015-08-17 01:26:00"),
                           admission2))

#> # A tibble: 3 x 3
#>   admission2          time_lorazepam2     admission_to_lorazepam
#>   <dttm>              <dttm>                               <dbl>
#> 1 2015-08-05 01:52:00 2015-08-05 02:57:00                     65
#> 2 2015-08-17 01:26:00 2015-08-17 01:26:00                    -34
#> 3 2016-02-25 18:59:00 2016-02-25 20:15:00                     76

Or you could just change all dates with a negative admission_to_lorazepam:

dat %>% mutate(admission2 = if_else(admission_to_lorazepam < 0,
                           time_lorazepam2 ,
                           admission2))
#> # A tibble: 3 x 3
#>   admission2          time_lorazepam2     admission_to_lorazepam
#>   <dttm>              <dttm>                               <dbl>
#> 1 2015-08-05 01:52:00 2015-08-05 02:57:00                     65
#> 2 2015-08-17 01:26:00 2015-08-17 01:26:00                    -34
#> 3 2016-02-25 18:59:00 2016-02-25 20:15:00                     76

Created on 2022-07-29 by the reprex package (v2.0.1)

Related