【问题标题】:Converting character dates into Julian day将字符日期转换为儒略日
【发布时间】:2018-11-01 13:58:12
【问题描述】:
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))

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

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 转换为一年中的儒略日?

【问题讨论】:

  • @Jaap's answer to your previous question 是否仍然相关(“请注意,即使是闰年,我也假设一年有 365 天”)?无需拆分“doy”,只需在 as.Date 中的 format 规范中使用文字 _。那就看strptime提取julian day吧。
  • 其实是的。在匿名发布答案之前,我几乎自己设法找到了解决方案。谢谢

标签: r date lubridate


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 2015-11-08
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多