0

I am trying to convert a duration between 2 dates in days with lubridate package in R and getting some error messages. I tried the following codes:

tbls_data_new1<- tbls_data_new %>% mutate(days = interval(end,start),
                                    days = as.period(days) ) 

The codes above work and I am getting the interval in year-month-day-time.... I want to convert this in days and tried the following:

I am getting results like that:
-358084800d 0H 0M 0S

Here is the data head from the portion that works. Thanks for the help

 structure(list(name_birtha_death = c("Toussaint Louverture(1743-1803)", 
"Jean-Jacques Dessalines(1758-1806)", "Henri Christophe(1767-1820)", 
"Alexandre Pétion(1770-1818)", "Jean-Pierre Boyer(1776-1850)", 
"Jean-Pierre Boyer(1776-1850)"), start = structure(c(-65378, 
-60631, -59611, -59611, -55429, -54496), class = "Date"), end = structure(c(-61236, 
-60366, -57988, -55430, -54496, -46343), class = "Date"), days = new("Period", 
    .Data = c(0, 0, 0, 0, 0, 0), year = c(-11, 0, -4, -11, -2, 
    -22, -1, 0, 0, 0, -2, -8, 0, -2, -4, -1, -3, 0, -8, 0, 0, 
    0, -6, -6, 0, -5, -2, 0, 0, 0, 0, 0, 0, -6, -8, 0, -10, -4, 
    0, -3, 0, -6, 0, 0, 0, 0, -13, -14, -2, 0, 0, -1, 0, 0, 0, 
    -1, -1, -5, -3, -2, -5, -5, -4, 0, -4), month = c(-4, -8, 
    -5, -5, -6, -3, 0, -11, -11, -11, -5, -1, -1, -7, -4, -10, 
    -2, -2, -10, -2, -10, -1, -5, -1, -6, -11, -7, -11, -8, -8, 
    -8, -3, -5, -9, 0, -6, -5, -7, -7, -8, -6, 0, -1, -1, 0, 
    -4, -5, -9, 0, -4, -2, -5, -10, -7, -8, -3, -3, 0, 0, -2, 
    0, 0, -8, -11, -5), day = c(-5, -21, -11, -12, -19, -26, 
    -29, -12, -8, -4, -24, -22, -13, -23, -18, -1, -24, -7, -8, 
    -6, -7, -25, -7, -12, -22, -12, -28, -24, -25, -15, -21, 
    -15, -3, -3, 0, -3, -27, -27, -5, -25, -27, -6, -22, -23, 
    -20, -8, -30, -16, 0, -13, -27, -23, -25, -22, -11, -27, 
    -26, 0, -22, -14, 0, 0, -24, -22, 0), hour = c(0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), minute = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x000001fea26b1ef0>)

I would appreciate any help.

1 Answers1

0

Counting difference in days between two dates using lubridate from @Oliver solved the issue

tbls_data_new$Days= tbls_data_new$end - tbls_data_new$start

Time differences in days
 [1] 4142  265 1623 4181  933 8153  395  347  342  341  908 2972   43  968
[15] 1599  671 1180   68 3235   67  311   55 2350 2232  205 2173  970  359
[29]  267  260  263  107  153 2468 2922  187 3831 1702  217 1363  210 2198
[43]   53   54   20  130 4929 5406  730  134   89  539  331  234  255  484
[57]  483 1827 1117  805 1826 1826 1730  359 1611