Search code examples
rdatetimedatediff

Error "longer object length is not a multiple of shorter object length" when using difftime


I'm calculate the difference in seconds of two consecutive row with the following code

set.seed(79)

library(outbreaks)
library(lubridate)

# Import data
disease_df <- measles_hagelloch_1861[, 3, drop = FALSE]

# Generate a random time for each day
disease_df$time <- sample(1:86400, nrow(disease_df), replace = TRUE)
disease_df$time <- hms::as.hms(disease_df$time)

# Combine date and time
disease_df$time1 <- with(disease_df, ymd(date_of_prodrome) + hms(time))

# Sort data
disease_df <- disease_df[order(disease_df$time1), ]

# Difference in days of two consecutive row
disease_df$diff <- as.numeric(difftime(disease_df$date_of_prodrome,
                                       dplyr::lag(disease_df$date_of_prodrome, 1), units = 'days'))

# Difference in seconds of two consecutive row
disease_df$diff1 <- as.numeric(difftime(disease_df$time1,
                                       dplyr::lag(disease_df$time1, 1), units = 'secs'))

Here is the resulted dataframe

enter image description here

and error message longer object length is not a multiple of shorter object length.

Could you please explain why difftime works fine for days but results in error for seconds? Thank you so much!


Solution

  • time1 column is of type "POSIXlt". I am not really sure why difftime with units = 'secs' doesn't work but if you convert it to POSIXct, it works without any error.

    disease_df$time1 <- as.POSIXct(disease_df$time1)
    disease_df$diff1 <- as.numeric(difftime(disease_df$time1,
                                   dplyr::lag(disease_df$time1, 1), units = 'secs'))