简体   繁体   中英

convert characters to month and year ( date ) in R

How do I convert a string "Apr-16" to Month April of 2016 ?

I tired as.Date(x, format ..) , that isn't helping .

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

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

Or using yearmon from zoo

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

data

x <- "Apr-16"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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