How do I convert date to number of days in R

后端 未结 7 1651
囚心锁ツ
囚心锁ツ 2021-01-04 14:19

How do I convert date to number of days, starting from the first day of the year.

How do I convert the following to the expected result below?

   Dat         


        
7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 15:07

    Here's a solution using the lubridate package:

    library(lubridate)
    
    x <- c("02/01/2000", "20/02/2000", "12/12/2000", "13/01/2001")
    date <- dmy(x)
    
    days <- yday(date) - 1 # so Jan 1 = day 0 
    total_days <- cumsum(days)
    

提交回复
热议问题