r - Convert date without year into Julian day (number of days since start of the year) -


i have date need convert julian days. data have

date <- c("21-jul", "14-jul", "08-jul", "08-jul","16-jul") class(date)  [1] "character" 

i want convert date julian days. above dates, julian days be:

date 202,195,189,189,197  

i found function as.posixlt converts date julian day. e.g.

tmp <- as.posixlt("16jun10", format = "%d%b%y") tmp$yday # [1] 166 

but needs date in particular order including year not have. there way convert dates characters , not have year in julian?

also, year not leap year.

julian days imply day of year 1st jan 1, 2nd jan 2 , on....

base r has function julian can produce looking paste0 , as.date.

julian(as.date(paste0("1970-", date), format="%y-%d-%b")) [1] 201 194 188 188 196 attr(,"origin") [1] "1970-01-01" 

note counted number of days january 1, 1970 (it starts @ 0), can add 1 desired result.

julian(as.date(paste0("1970-", date), format="%y-%d-%b")) + 1 [1] 202 195 189 189 197 

i manually checked 1970 not leap year:

seq(as.date("1970-02-25"), as.date("1970-03-02"), by="day") [1] "1970-02-25" "1970-02-26" "1970-02-27" "1970-02-28" "1970-03-01" "1970-03-02" 

for future reference, possible set starting point yourself, using origin argument as.date:

julian(as.date(paste0("1980-", date), format="%y-%d-%b"), origin=as.date("1980-01-01")) [1] 202 195 189 189 197 attr(,"origin") [1] "1980-01-01" 

as should clear, origin can set date wish, used calculate number of days following (or preceding) date.


Comments

Popular posts from this blog

Formatting string according to pattern without regex in php -

c - zlib and gdi32 with OpenSSL? -

java - inputmismatch exception -