R Excercise Problem - Date data conversion

Hello Experts,
I am learning R. In one of the online learning courses, I came across the following exercise that I solved, however, Katacoda web platform says the result is NOT correct. I wanted to double-check if I am missing something.
Problem:

Consider a data frame strDates having values 01/05/1965, 08/16/1975. Convert this to different date formats like month/date/year, year-month-date.

My Code:
dts <- c("01/05/1965", "08/16/1975")
strDates <- data.frame (dts)
dts1 <- format (as.Date(strDates$dts, format="%m/%d/%Y"), "%m/%d/%Y")
dts2 <- format (as.Date(strDates$dts, format="%m/%d/%Y"), "%Y-%m-%d")

strDates <- data.frame (dts1)
strDates

strDates <- data.frame (dts2)
strDates

Online Course: FrecoPlay (TCS) Lesson: Data Cleansing Using R, Platform: Katacoda

#Homework

See the lubridate package for functions to easily convert character strings in most formats to date objects.

Iā€™d give a reprex, but Iā€™m on a tablet

Example

datestring <- ā€œ03/22/2020ā€
mdy(datestring)
2 Likes

Seems fine to me. The web platform might be overly opinionated....
Also I like technocrats suggestion of using lubridate. It's a great package that makes a lot of date related work easier.

1 Like

I think lubridate is a really great package to work with date format. In my experience, it is always great idea to use consistent format such as yyyy-mm-dd. It will work well with SQL and CSV data as well

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.