【问题标题】:R: Extracting year and month from a numeric date [duplicate]R:从数字日期中提取年份和月份[重复]
【发布时间】:2016-02-10 06:01:18
【问题描述】:

我有这样的数字模式日期: 2012 年 5 月 20 日

从中提取年份和月份的最优雅的方法是什么?

【问题讨论】:

  • 可能与lubridate,例如lubridate::month(lubridate::dmy("20/05/2012"))lubridate::year(lubridate::dmy("20/05/2012")).
  • 如果你需要名字 weekdays(as.Date("20/05/2012", format = "%d/%m/%Y")) [1] "Sunday" 月份(as. Date("20/05/2012", format = "%d/%m/%Y")) [1] "五月"

标签: r date type-conversion


【解决方案1】:

首先需要将其转换为日期对象:

x <- as.Date("20/05/2012", format="%d/%m/%Y")

然后得到月份号:

format(x,"%m")

并获取年份编号:

format(x,"%Y")

如果您想要月份名称(注意,这是您的机器语言):

months(x)

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2021-10-02
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多