Search code examples
rjulian-date

Convert date to julian


I am trying to convert a date into a julian date. I have this data frame

      diff   yr mo dy hr
1 3.174583 1873 12  1  1
2 2.874583 1873 12  1  3
3 2.574583 1873 12  1  5
4 2.074583 1873 12  1  7
5 1.774583 1873 12  1  9
6 1.474583 1873 12  1 11

and for every row I want to assign the number of days since a particular date (e.g. "1870-01-01").

I tried like this

julian(as.Date(paste(dep$yr,dep$mo,dep$dy,sep="/"),"%Y/%m/%d")) 

but this gives me the days since "1970-01-01".. If I try to set the origin

  julian(as.Date(paste(dep$yr,dep$mo,dep$dy,sep="/"),"%Y/%m/%d"),origin = paste(1870,1,1,sep="-"))

then I get this error

Error in unclass(x) - unclass(origin) : 
  non-numeric argument to binary operator

Can some one help me?

Many thanks


Solution

  • Using ISOdate here and a proper origin date value should help you

    julian(with(dep, ISOdate(yr,mo,dy,hr)), origin=as.Date("1870-1-1"))
    
    # Time differences in days
    # [1] 1430.042 1430.125 1430.208 1430.292 1430.375 1430.458