r - 从字符年份中提取月份并在R中使用doy
问题描述
给定一年零一天(儒略日),我如何提取月份?例如
Year <- '2000'
Doy <- '159'
我想提取上述年份和 Doy 的月份。我想首先我会将其转换为日期,然后使用从中提取月份format(mydate,"%m")
# first convert into date and then extract the month
as.Date(paste0(Year'-',Doy), format = '%Y-%d')
NA
这给了我 NA。
解决方案
%d 代表月份中的某天。%j 是一年中的一天,其中 Jan 1 是一年中的一天,一月 2 是一年中的一天,...,十二月 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