简体   繁体   English

将字符转换为R中的月份和年份(日期)

[英]convert characters to month and year ( date ) in R

How do I convert a string "Apr-16" to Month April of 2016 ? 我如何将字符串转换为“APR-16”到Month的2016年4月?

I tired as.Date(x, format ..) , that isn't helping . 我累了as.Date(x, format ..) ,这没有帮助。

What options do I have here ? 我在这里有什么选择?

x <- "Apr-16"
x <- as.Date(paste0("1-",x),format="%d-%b-%y")
format(x,"%B of %Y")
[1] "April of 2016"

Another option is regex 另一个选择是regex

sub("-", "il of 20", x)
#[1] "April of 2016"

Or using yearmon from zoo 或使用zoo yearmon

library(zoo)
format(as.Date(as.yearmon(x, "%b-%y")), "%B of %Y")
#[1] "April of 2016"

data 数据

x <- "Apr-16"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM