Convert a date vector into Julian day in R

后端 未结 5 1126
故里飘歌
故里飘歌 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:46

    Try the following to convert from class character(i.e. text) to class POSIXlt, and then extract Julian day (yday):

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

    For more details on function settings:

    ?POSIXlt
    ?DateTimeClasses
    

    Another option is to use a Date class, and then use format to extract a julian day (notice that this class define julian days between 1:366, while POSIXlt is 0:365):

    tmp <- as.Date("16Jun10", format = "%d%b%y")
    format(tmp, "%j")
    # [1] "167"
    
    0 讨论(0)
  • 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(12<mm){mm<- mm-12}
        iyyy<- floor(jc-4715)
        if(mm>2){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)
    }
    
    0 讨论(0)
  • 2020-11-30 11:48
    my.data = read.table(text = "
         OBS  MONTH1  DAY1  YEAR1
           1       3     1   2012
           2       3    31   2012
           3       4     1   2012
           4       4    30   2012
           5       5     1   2012
           6       5    31   2012
           7       6     1   2012
           8       6    30   2012
           9       7     1   2012
          10       7    31   2012    
    ", header = TRUE, stringsAsFactors = FALSE)
    
    my.data$MY.DATE1 <- do.call(paste, list(my.data$MONTH1, my.data$DAY1, my.data$YEAR1))
    my.data$MY.DATE1 <- as.Date(my.data$MY.DATE1, format=c("%m %d %Y"))
    
    my.data$my.julian.date <- as.numeric(format(my.data$MY.DATE1, "%j"))
    my.data
    

    Returns, which technically is incorrect since Julian dates do not return to 1 on the first day of each January:

    http://en.wikipedia.org/wiki/Julian_day

    The dates below are Ordinal dates:

       OBS MONTH1 DAY1 YEAR1   MY.DATE1 my.julian.date
    1    1      3    1  2012 2012-03-01             61
    2    2      3   31  2012 2012-03-31             91
    3    3      4    1  2012 2012-04-01             92
    4    4      4   30  2012 2012-04-30            121
    5    5      5    1  2012 2012-05-01            122
    6    6      5   31  2012 2012-05-31            152
    7    7      6    1  2012 2012-06-01            153
    8    8      6   30  2012 2012-06-30            182
    9    9      7    1  2012 2012-07-01            183
    10  10      7   31  2012 2012-07-31            213
    
    0 讨论(0)
  • 2020-11-30 11:49

    You can use R's insol package which has a JD(x, inverse=FALSE) function which converts POSIXct to Julian Day Number (JDN).

    insol package also has JDymd(year,month,day,hour=12,minute=0,sec=0) for custom dates.

    To display the whole Julian Date (JD) you possibly have to set options(digits=16).

    0 讨论(0)
  • 2020-11-30 11:50

    Similarly:

    require(lubridate)
    x = as.Date('2010-06-10')
    yday(x)
    
    [1] 161
    

    Also note, using lubridate:

    > dmy('16Jun10')
    [1] "2010-06-16 UTC"
    
    0 讨论(0)
提交回复
热议问题