将字符日期转换为儒略日

时间:2018-11-01 13:58:12

标签: r date lubridate

dat <- structure(list(crop_name = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
                         "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", 
                         "b", "b"), 
       year = c(2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L,  2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L,  2010L, 2010L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L), doy = c("Sep_24", "Oct_1", "Oct_8", "Oct_15", "Oct_22", "Oct_29", "Nov_5", "Nov_12", "Nov_19", "Nov_26", "Dec_3", "Sep_30", "Oct_7", "Oct_14", "Oct_21", "Oct_28", "Nov_4", "Nov_11", "Nov_18", "Nov_25", "Dec_2", "Jan_22", "Jan_29", "Feb_5", 
                  "Feb_12", "Feb_19", "Feb_25", "March_5", "March_10", "March_19",
                   "Jan_14","Jan_21", "Jan_28", "Feb_4", "Feb_11", "Feb_18", 
                   "Feb_25", "March_4","March_11", "March_18"), 
           per_f = c(1, 4, 13, 18, 30, 42, 68, 83, 93, 100, 100, 0, 0, 2, 8, 20, 35, 65, 78, 90, 100, 0, 2, 18, 26, 28, 70, 76, 84, 100, 1, 2, 10, 25, 50, 80, 92, 98, 100, 100)), 
          class = "data.frame", row.names = c(NA, -40L))

在上述数据中,我需要将doy列转换为儒略日,假设全年有365天。到目前为止,这是我设法做的事情:

doy.col <- as.data.frame(str_split_fixed(dat$doy, "_", 2))
dat$month <- doy.col$V1
dat$doy <- doy.col$V2
dat$date <- paste0(dat$doy,"/",dat$month,"/",dat$year)

我现在如何将字符dat$date隐藏到每年的朱利安日?

1 个答案:

答案 0 :(得分:1)

使用您当前拥有的内容,只需将其转换为日期,然后使用strftime指定儒略格式。

dat$date2 <- as.Date(dat$date, format = "%d/%b/%Y")
dat$doy <- strftime(dat$date2, format = "%j")
head(dat)

crop_name year doy per_f month        date      date2
1         a 2009 267     1   Sep 24/Sep/2009 2009-09-24
2         a 2009 274     4   Oct  1/Oct/2009 2009-10-01
3         a 2009 281    13   Oct  8/Oct/2009 2009-10-08
4         a 2009 288    18   Oct 15/Oct/2009 2009-10-15
5         a 2009 295    30   Oct 22/Oct/2009 2009-10-22
6         a 2009 302    42   Oct 29/Oct/2009 2009-10-29