user3206440
user3206440

Reputation: 5049

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 ?

Upvotes: 2

Views: 3619

Answers (2)

akrun
akrun

Reputation: 886938

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"

Upvotes: 1

Erdem Akkas
Erdem Akkas

Reputation: 2070

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

Upvotes: 4

Related Questions