How to convert date to number of days in R

How to convert a date to a number of days starting on the first day of the year.

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

Date 02/01/2000 20/02/2000 12/12/2000 13/01/2001 

Below is the expected result.

 Date NumDays TotalDays 02/01/2000 1 1 20/02/2000 51 51 12/12/2000 346 346 13/01/2001 13 379 
+6
source share
6 answers

Here is the 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) 
+7
source

Assuming you want to count January 1 of the year as 0, we get:

 DF <- data.frame(Date = c("02/01/2000", "20/02/2000", "12/12/2000", "13/01/2001")) DF$Date <- as.Date(DF$Date, "%d/%m/%Y") Diff <- function(x, start) as.numeric(x - as.Date(cut(start, "year"))) transform(DF, NumDays = Diff(Date, Date), TotalDays = Diff(Date, Date[1])) 

which gives:

  Date NumDays TotalDays 1 2000-01-02 1 1 2 2000-02-20 50 50 3 2000-12-12 346 346 4 2001-01-13 12 378 

If you want to read on January 1 1, then add 1 to the expression in Diff .

UPDATE: Correction.

UPDATE: Added DF definition to make it standalone.

UPDATE: we add a run using the data in the comment below.

 > DF <- data.frame(Date = as.Date(c("1980-01-03", "1980-01-04", "1980-01-05", + "1980-01-07", "1980-01-10", "1980-01-16"))) > > Diff <- function(x, start) as.numeric(x - as.Date(cut(start, "year"))) > transform(DF, NumDays = Diff(Date, Date), TotalDays = Diff(Date, Date[1])) Date NumDays TotalDays 1 1980-01-03 2 2 2 1980-01-04 3 3 3 1980-01-05 4 4 4 1980-01-07 6 6 5 1980-01-10 9 9 6 1980-01-16 15 15 
+3
source

Upload your dataset

 df <- structure(list(Date = structure(c(1L, 4L, 2L, 3L), .Label = c("02/01/2000", "12/12/2000", "13/01/2001", "20/02/2000"), class = "factor"), Date2 = structure(c(10958, 11007, 11303, 11335), class = "Date"), NumDays = structure(c(1, 50, 346, 378), units = "days", class = "difftime")), .Names = c("Date", "Date2", "NumDays"), row.names = c(NA, -4L), class = "data.frame") 

Date format:

 startdate <- as.Date("01/01/2000","%d/%m/%Y") df$Date2 <- as.Date(df$Date,"%d/%m/%Y") 

Use difftime to calculate the difference in days.

 df$NumDays <- difftime(df$Date2,startdate ,units="days") df Date Date2 NumDays # 1 02/01/2000 2000-01-02 1 days # 2 20/02/2000 2000-02-20 50 days # 3 12/12/2000 2000-12-12 346 days # 4 13/01/2001 2001-01-13 378 days 
+3
source
 startvalue <- "01/01/2000" dt <- data.table( datevalue <- c("13/01/2001","12/12/2000") ) DateFormat <- "%d/%m/%Y" dt[,datevalue := as.Date(datevalue,DateFormat)] startvalue <- as.Date(startvalue,DateFormat) dt[,TotalDays := datevalue - startvalue] dt[,Jan01 := as.Date(paste0('01/01/',strftime(datevalue,'%Y')),DateFormat)] dt[,NumDays := datevalue - Jan01] 
+1
source

I think this will help:

Use as.Date()

Example:

 one <- as.Date(c("02/01/2000", "01/01/2000")) 

number of days between 02/01/2000 and 02/01/2000:

 days <- one[1] - one[2] 
+1
source

The date formatting flag %j will give you a day of the year starting at 0.

 d <- read.table(text='Date 02/01/2000 20/02/2000 12/12/2000 13/01/2001', header=TRUE) d<-transform(d, NumDays=as.numeric(strftime(as.Date(Date, format='%d/%m/%Y'), '%j'))-1) # Date NumDays # 1 02/01/2000 1 # 2 20/02/2000 50 # 3 12/12/2000 346 # 4 13/01/2001 12 

Then, to add TotalDays , you can use cumsum with some modular arithmetic,

 transform(d, TotalDays=cumsum(c(1, ifelse(diff(NumDays) > 0, diff(NumDays), diff(NumDays) %% 365 + 1)))) # Date NumDays TotalDays # 1 02/01/2000 1 1 # 2 20/02/2000 50 50 # 3 12/12/2000 346 346 # 4 13/01/2001 12 378 

Or use this shorter alternative.

 transform(d, TotalDays=cumsum(c(1, diff(as.Date(Date, format='%d/%m/%Y'))))) 
+1
source

Source: https://habr.com/ru/post/956637/


All Articles