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)