从字符年中提取月份并在 R 中执行

extract month from a character year and doy in R

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

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

这给了我 NA。

%d 是一个月中的第几天。 %j 是一年中的一天,其中 1 月 1 日是第 1 年的一天,1 月 2 日是第 2 年的一天,...,12 月 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