【问题标题】:extract month from a character year and doy in R从字符年份中提取月份并在R中使用doy
【发布时间】:2021-04-06 11:20:55
【问题描述】:

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

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。

【问题讨论】:

  • 看起来你只是在paste0(Year,'-',Doy) 中缺少一个逗号,修复它对我有用。 &gt; as.Date(paste0(Year,'-',Doy), format = '%Y-%d') [1] "2000-04-15"

标签: r date lubridate


【解决方案1】:

%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

【讨论】:

    猜你喜欢
    • 2021-09-11
    • 2021-10-02
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    相关资源
    最近更新 更多