简体   繁体   中英

Subtracting dates in the same row by a factor. R

I have the following data frame:

DF<-data.frame(stringsAsFactors = TRUE,
           Sample = c(rep("s1",4),rep("s2",4)),
           date = c("21/07/2020","24/07/2020","25/07/2020","27/07/2020",
                    "03/08/2020","06/08/2020","09/08/2020","10/08/2020"))

First I want to obtain the number of days between consecutive dates by the factor "Sample". so the output would be like this:

DF_2<-data.frame(stringsAsFactors = TRUE,
           Sample = c(rep("s1",4),rep("s2",4)),
           date = c("21/07/2020","24/07/2020","25/07/2020","27/07/2020",
                    "03/08/2020","06/08/2020","09/08/2020","10/08/2020"),
           days = c(NA,3,1,2,NA,3,3,1))

Where variable "days" is my outcome variable. Afterwards I want to add all those "days" by factor. But that is easy, will do it like this:

df_3<-aggregate(days~Sample,DF_2,sum)

I would much appreciate it if someone helps me to get right first step, to get DF_2.

We can use diff to get the difference between Date class converted 'date' column

library(dplyr)
library(lubridate)
DF1 <- DF %>%
         mutate(date = dmy(date)) %>% 
         group_by(Sample) %>% 
         mutate(days = c(NA, diff(date))) %>%
         ungroup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM