3

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

zx8754
  • 42,109
  • 10
  • 93
  • 154
Daniel Murran
  • 45
  • 1
  • 2
  • 7

2 Answers2

5

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).

Julius Vainora
  • 44,018
  • 9
  • 79
  • 96
  • Great Thank you! it also gave me a good understanding of the code – Daniel Murran Nov 07 '18 at 13:47
  • @JuliusVainora Thanks for this! I tried this on my data with the format "12/18/1996 0:00:00" using ```mdy_hms()``` as that's my input format but my column was turned into all NAs. Can you help clarify as to why that is happening? I appreciate you taking the time! – Salma Abdel-Raheem May 24 '21 at 03:18
2

Using anytime:

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

library(anytime)

anydate(x)
# [1] "2015-01-15"
zx8754
  • 42,109
  • 10
  • 93
  • 154