R convert dates into Julian days in a loop -
i have date year 1996 till 2014 need convert julian days. here example data:
date<- c("21-jul", "14-jul", "08-jul", "08-jul","16-jul","22-jul", "10-jul", "02-jul", "06-jul","18-jul","24-jul", "15-jul", "03-jul", "04-jul","19-jul") year<-rep(1996:1998,each=5) dat<-as.data.frame(cbind(date,year)) dat$date<-as.character(dat$date)
for each year, want convert date julian day i.e. day of year. used following function link:
convert date without year julian day (number of days since start of year)
for leap-year (e.g. 1996), can convert date julian day following:
julian(as.date(paste0("1996-", ds$ds), format="%y-%d-%b"), origin=as.date("1996-01-01"))+1
for non-leap year (e.g. 1997), can convert date julian day following:
julian(as.date(paste0("1997-", ds$ds), format="%y-%d-%b"), origin=as.date("1997-01-01"))
now, want in loop period 1996 2014. did this:
install.packages("lubridate") # function detecting leap year library("lubridate") years <- 1996:2014 leap<-years[leap_year(years)] for(i in 1996:2014){ if(i %in% leap){ julian(as.date(paste0(i,"-", ds$ds), format="%y-%d-%b"), origin=as.date(i,"-01-01"))+1 } else { julian(as.date(paste0(i,"-", ds$ds), format="%y-%d-%b"), origin=as.date(i,"-01-01")) }} error in chartodate(x) : character string not in standard unambiguous format
i think error happening because of paste0(i,"-", ds$ds)
, origin=as.date(i,"-01-01"))
. know how address it?
why don't use posixlt
class contains julian days since start of year 1 of components?
as.posixlt(as.date(paste(dat$year, dat$date, sep = "-"), "%y-%d-%b"))$yday #[1] 202 195 189 189 197 202 190 182 186 198 204 195 183 184 199
Comments
Post a Comment