如何从 R 中的年月(2017-10)类型对象中提取月份? [英] How to extract month from a Year-Month (2017-10) type object in R?

查看:8

问题描述

这个对象是字符格式的,我想避免通过基于字符串的函数提取 10 个,因为它非常麻烦.如果有办法通过 as.Date() 和某种特定格式,我很乐意使用它

This object is in character format and i would like to avoid extracting the 10 by string based functions since it is very cumbersome. If there is a way through as.Date() and some particular format, i would be happy to use that

推荐答案

我相信这个问题之前已经在 SO 上问过了.

I believe this question has been asked before on SO.

作为 anytime 包的替代方案,您可以使用 lubridate

As an alternative to the anytime package you may use lubridate

library(lubridate)
format(ymd("2017-10", truncated = 1L), "%m")

或以 R 为基数

format(as.Date(paste0("2017-10", "-01")), "%m")
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆