从 R 中的字符年份和 doy 中提取月份

时间:2021-04-06 11:20:55

标签: r date lubridate

给定一年零一天(儒略日),我如何提取月份?例如

Year <- '2000'
Doy  <- '159'

我想提取上述 Year 和 Doy 的月份。我想首先将其转换为日期,然后使用 format(mydate,"%m")

从中提取月份
# first convert into date and then extract the month  
as.Date(paste0(Year'-',Doy), format = '%Y-%d')
NA

这让我不知道。

1 个答案:

答案 0 :(得分:1)

%d 是一个月中的某一天。 %j 是一年中的第 1 天,其中 Jan 1 是第 1 年的天,Jan 2 是第 2 年的天,...,Dec 31 是第 365 天(或闰年的 366 天)。有关百分比代码,请参阅 ?strptime。

Year <- '2000'
Doy  <- '159'

date <- as.Date(paste(Year, Doy), format = "%Y %j"); date
## [1] "2000-06-07"

as.numeric(format(date, "%m")) # month number
## [1] 6