Calculate difference between two dates

后端 未结 1 531
眼角桃花
眼角桃花 2021-01-24 11:24

My dates in the txt file are like, start date= \"011589\" and end date is \"122390\". How can I calculate the duration using R.

I tried mydata$startdate=as.Date(mydata

相关标签:
1条回答
  • 2021-01-24 12:11

    This can be done using format

    Start <- as.Date("011589", "%m%d%y")
    Start
    #[1] "1989-01-15"
    End <- as.Date("122390", "%m%d%y")
    End
    #[1] "1990-12-23"
    

    If we need the difference in 'days'

    as.vector(difftime(End, Start, units='days'))
    #[1] 707
    

    Or just use - to get the difference in 'days'. The above method is more flexible as we can specify the units

    as.vector(End-Start)
    #[1] 707
    
    0 讨论(0)
提交回复
热议问题