Convert a date vector into Julian day in R

后端 未结 5 1156
故里飘歌
故里飘歌 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条回答
  •  旧时难觅i
    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
    

提交回复
热议问题