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

查看:55
本文介绍了如何从R中的Year-Month(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.

可以随时使用 lubridate

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

或以R为底基

format(as.Date(paste0("2017-10", "-01")), "%m")

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

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