calculate difference between 2 dates and print the between dates

56 Views Asked by At
    st_day<-c(1,5,10)
endday<-c(4,9,15)
d<-c(1,2,3)
data<-cbind(st_day,endday,d)

days1<-c(1:15)
dose1<-rep(c(1,2,3),each=5)
result <- cbind(days1,dose1)

Hello I have 2 coulmns with starts and end dates and corresponding dose with was a administered for certain duration. How can compute the difference and print out the duration between these two dates. I have given a sample code the expected result.

Thank you.

1

There are 1 best solutions below

7
On

We can use Map to get the corresponding sequence of the two vectors into a list, then cbind the unlistted 'lst' and the replicated 'd' (based on the lengths of 'lst'

lst <- Map(`:`, st_day, endday)
out <- cbind(unlist(lst), rep(d, lengths(lst)))