Calculate difference between dates in columns

Hi All!

I'm trying to get the difference between the dates in one line. The following code will output just the dates form an imported CSV.

updatedataraw[,5:ncol(updatedataraw)]

Every Line has an individual number of Columns containig dates. the "datelist" will beginn at column 5. There could be an infinite number of date-columns, the CSV this time has 84 Columns but there could be more soon.

Here is a snippet

1  31.10.2021  16.07.2021  15.10.2020  11.10.2019  17.04.2019                                                                                                 
2  26.10.2021  20.09.2021  23.08.2021  16.07.2021  15.07.2021  15.10.2020  11.10.2019  17.04.2019  25.01.2019                                    
3  28.10.2021  30.09.2021  23.08.2021  16.07.2021  15.07.2021  15.10.2020  11.10.2019  17.04.2019  25.01.2019                                    
4  28.10.2021  30.08.2021  16.07.2021  15.10.2020  11.10.2019  17.04.2019  25.01.2019 
...                              

I'd now like to calculate the number of days between each of the dates in each line. I then use all these numbers to do my data analysis, so theres no need to put them back into the original table.

Has anyone an idea how to approach this data transformation? Or is there an example of this problem somewhere?

Thx for your advice!!

The first thing to do is to convert the data from strings to date objects, then do the differences.

suppressPackageStartupMessages({
  library(lubridate)
})

get_diff <- function(x,y) time_length(x %--% y, unit = "day")

date1 <- dmy("31.10.2021")
date2 <- dmy("12.11.2021")

get_diff(date1,date2)
#> [1] 12

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.