【发布时间】: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吧。 -
其实是的。在匿名发布答案之前,我几乎自己设法找到了解决方案。谢谢