0

I would like to use difftime() to extract the difference between two date/time variables which are as.posixct. But sometimes one (or both) of the values are missing (NA), just like below.

Start time             Antibiotic time
2016-06-28 08:36:00    NA
2019-10-30 10:43:00    2019-10-30 10:11:56
NA                     NA

I want: start time - antibiotic time Like: Antibiotica$ABS <- difftime(Antibiotica$StartTime, Antibiotica$AntibioticTime, units=c("mins"), na.rm=TRUE)

But now, I get an error. I think it is because of the wrong use of na.rm=TRUE. How to add this in the right way?

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
Yvonne
  • 21
  • 3
  • 1
    What is the expected result? What is `na.rm = TRUE` supposed to achieve? You can't calculate a time difference from only one time. – Roland Jan 31 '22 at 14:29

1 Answers1

0

As Roland points out in the comments, it's not clear that you should remove NA values. If there is a start time but antibiotic time is NA, then the time difference should also be NA. If both times are NA, then again the time difference should be NA

If you were to remove all the NA values in the resulting difftime, then you will only get results for those rows with complete data, but then these will no longer match up to your Antibiotica data frame. In your little example data frame for example, you would only get a single non-NA result. How would you store that in a column?

From your example, your code should work like this:

Antibiotica$ABS <- difftime(Antibiotica$StartTime, Antibiotica$AntibioticTime)

Antibiotica
#>             StartTime      AntibioticTime           ABS
#> 1 2016-06-28 08:36:00                <NA>       NA mins
#> 2 2019-10-30 10:43:00 2019-10-30 10:11:56 31.06667 mins
#> 3                <NA>                <NA>       NA mins

If you're not getting this result, you might need to make sure that your columns are in an actual date-time format (e.g. ensure class(Antibiotica$StartTime) is not "character").

If, once you have the calculation and you only want to have complete cases, you can do

Antibiotica[complete.cases(Antibiotica),]
#>             StartTime      AntibioticTime
#> 2 2019-10-30 10:43:00 2019-10-30 10:11:56

Data used

Antibiotica <- structure(list(StartTime = structure(c(1467102960, 1572432180, NA), 
  class = c("POSIXct", "POSIXt"), tzone = ""), 
  AntibioticTime = structure(c(NA, 1572430316, NA), 
  class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, -3L), 
  class = "data.frame")

Antibiotica
#>             StartTime      AntibioticTime
#> 1 2016-06-28 08:36:00                <NA>
#> 2 2019-10-30 10:43:00 2019-10-30 10:11:56
#> 3                <NA>                <NA>

Created on 2022-01-31 by the reprex package (v2.0.1)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87