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

查看:21
本文介绍了如何从 R 中的年月(2017-10)类型对象中提取月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个对象是字符格式的,我想避免通过基于字符串的函数提取 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")

这篇关于如何从 R 中的年月(2017-10)类型对象中提取月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆