user18143
user18143

Reputation: 171

How do I convert date to number of days in R

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?

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

Below is 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

Upvotes: 15

Views: 32670

Answers (7)

Elite
Elite

Reputation: 15

You can also use this solution to get the number of the days :

mydates <- as.Date(c("2007-06-22", "2004-02-13")) days <- mydates[1] - mydates[2] days <- as.numeric(days) [1] 1225

Upvotes: 0

hadley
hadley

Reputation: 103898

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)

Upvotes: 17

G. Grothendieck
G. Grothendieck

Reputation: 269644

Assuming that you wish 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 count January 1st as 1 then add 1 to the expression in Diff.

UPDATE: Correction.

UPDATE: Added DFdefinition to make it self contained.

UPDATE: We add a run using data in 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

Upvotes: 3

Matthew Plourde
Matthew Plourde

Reputation: 44614

The %j datetime formatting flag will give you the 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 the 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')))))

Upvotes: 1

Jonas Tundo
Jonas Tundo

Reputation: 6197

Load 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")

Format dates:

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

Upvotes: 11

Vinayak Pahalwan
Vinayak Pahalwan

Reputation: 3005

I guess 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]

Upvotes: 2

TheComeOnMan
TheComeOnMan

Reputation: 12875

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]

Upvotes: 1

Related Questions