Home > Mobile >  unable to change date format - R
unable to change date format - R

Time:10-17

The date is currently yyyy-mm-dd, but I want it as mm/dd/yyyy.

I've tried various methods which either result in absolutely nothing happening or getting errors such as:

Warning message: Problem while computing date = lubridate::mdy(date). ℹ All formats failed to parse. No formats found.

Error in mutate(): ! Problem while computing date = format(date, "%m/%d/%Y"). Caused by error in format.default(): ! invalid 'trim' argument Backtrace:

  1. mass %>% mutate(date = format(date, "%m/%d/%Y"))
  2. base::format.default(date, "%m/%d/%Y")

How do I fix this?

library(dplyr)
library(lubridate)

mass = read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/mass_killing_incidents_public.csv')

mass2 = mass
mass2$date = format(mass$date, format = "%m/%d/%Y")

mass2 = mass %>%
  mutate(date = lubridate::mdy(date),
         date = strftime(date, "%m/%d/%Y"))

mass2 = mass %>% 
  mutate(date = format(date, '%m/%d/%Y'))

CodePudding user response:

We could do

library(dplyr)
library(lubridate)
mass %>%
  mutate(date = lubridate::ymd(date), date = strftime(date, "%m/%d/%Y"))
  • Related