Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Character Column to Date in R with Lubridate

Tags:

date

r

lubridate

I have a character column called date that holds data such as "January 15, 2015" I am trying to convert it to ymd format with the as.date function and I also tried the lubridate package. I have tried methods such as:

shootings$Date <- ymd(shootings$Date))

and

shootings$Date <- as.Date(as.character(shootings$Date))

I Would like it to look like 2013-01-01 (ymd) if possible. but I keep getting a parsing issue or the data just goes in N/A. Any help would be appreciated

like image 423
Daniel Murran Avatar asked Jan 27 '23 04:01

Daniel Murran


2 Answers

What you want is

mdy("January 15, 2015")
# [1] "2015-01-15"

The problem is that the function name has to correspond to your input format (mdy), not the desired output (ymd).

like image 71
Julius Vainora Avatar answered Jan 30 '23 22:01

Julius Vainora


Using anytime:

# example data
x <- "January 15, 2015"

library(anytime)

anydate(x)
# [1] "2015-01-15"
like image 30
zx8754 Avatar answered Jan 30 '23 23:01

zx8754