如何从 R 中的年月(2017-10)类型对象中提取月份? [英] How to extract month from a Year-Month (2017-10) type object in R?
这个对象是字符格式的，我想避免通过基于字符串的函数提取 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.
As an alternative to the
anytime package you may use
library(lubridate) format(ymd("2017-10", truncated = 1L), "%m")
或以 R 为基数
format(as.Date(paste0("2017-10", "-01")), "%m")