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
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]
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]
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
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 DF
definition 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
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
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)