Convert a date vector into Julian day in R

后端 未结 5 1125
故里飘歌
故里飘歌 2020-11-30 11:03

I have a column of dates in the format: 16Jun10 and I would like to extract the Julian day. I have various years.

I have tried the functions julian and mdy.date an

5条回答
  •  有刺的猬
    2020-11-30 11:47

    Here are my R versions of code originally written in APL and converted to J. We call this pseudo-Julian because it is only intended for dates after October 15, 1582 which is when calendar reform, in some parts of the Western world, arbitrarily changed the date.

    #* toJulian: convert 3-element c(Y,M,D) timestamp into pseudo-Julian day number.
    toJulian<- function(TS3)
    {   mm<- TS3[2]
        xx<- 0
        if( mm<=2) {xx<- 1}
        mm<- (12*xx)+mm
        yy<- TS3[1]-xx
        nc<- floor(0.01*yy)
        jd<- floor(365.25*yy)+floor(30.6001*(1+mm))+TS3[3]+1720995+(2-(nc-floor(0.25*nc)))
        return(jd)
    #EG toJulian c(1959,5,24) -> 2436713
    #EG toJulian c(1992,12,16) -> 2448973
    }
    


    Here's the inverse function:

    #* toGregorian: convert pseudo-Julian day number to timestamp in form c(Y,M,D)
    # (>15 Oct 1582).  Adapted from "Numerical Recipes in C" by Press,
    # Teukolsky, et al.
    toGregorian<- function(jdn)
    {   igreg<- 2299161       # Gregorian calendar conversion day c(1582,10,15).
        ja<- floor(jdn)
        xx<- 0
        if(igreg<=ja){xx<- 1}
        jalpha<- floor((floor((xx*ja)-1867216)-0.25)/36524.25)
        ja<- ((1-xx)*ja) + ((xx*ja)+1+jalpha-floor(0.25*jalpha))
        jb<- ja+1524
        jc<- floor(6680+((jb-2439870)-122.1)/365.25)
        jd<- floor(365.25*jc)
        je<- floor((jb-jd)/30.6001)
        id<- floor((jb-jd)-floor(30.6001*je))
        mm<- floor(je-1)
        if(122){iyyy<- iyyy-1}
        if(0>iyyy){iyyy<- iyyy-1}
        gd<- c(iyyy, mm, id)
        return(gd)
    #EG toGregorian 2436713 -> c(1959,5,24)
    #EG toGregorian 2448973 -> c(1992,12,16)
    }
    

提交回复
热议问题