簡體   English   中英

如何構造一年中的年月日

[英]How to construct the date from year and day of the year

給出以下數據。

year <- rep(2016,3)
yday <- c(40, 100, 260)
date <- c('2016-02-09','2016-04-09','2016-09-16')

df <- data.frame(year=year, yday=yday, date=date)

我想根據年和年的日期列構造日期。

請參閱?strptime它列出了所有格式化日期的選項:

> as.Date(paste(yday,year), format="%j %Y")
#[1] "2016-02-09" "2016-04-09" "2016-09-16"

使用lubridate,你可以做

date <- as.POSIXct(paste0(year,"-01-01"))
yday(date) <- yday

date
# [1] "2016-02-09 CET"  "2016-04-09 CEST" "2016-09-16 CEST"

我們可以用as.Date生成的第一次約會year ,然后添加yday

as.Date(paste0(df$year, "-01-01")) + df$yday - 1

#[1] "2016-02-09" "2016-04-09" "2016-09-16"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM