0

So I have an excel sheet loaded into R with multiple dates. I need to convert everything into an actual date that can be read in R but the way the sheet came to me (I didn't make it), all the dates are messed and not readable by defaults. I also tried just fixing it in excel by changing the setting from "Text" to "Long Date" but didn't help change anything in R.

So far I have been able to separate one date column into three columns using the separate() function twice (once for the year and twice in a nested way for the month and day). The day and year columns are now set to an as.numeric(). Now all I have to do convert the screwed up months into something actually readable in R that can let me do a full date conversion.

I want to turn this into this:

Month Date        Readable Month
  Jan                  January
  Feb                  February
  Mar                  March
   .                     .
   .                     .
  July                 July
  June                 June

They're all chr in my dataframe. And there are a lot of columns I need to fix with this.

Once I can get this last step figured out, I'm planning on creating a function to automatically go through the process and create a new "date" column with dates that are readable in R. The "Full_Date" Column where I need to get to but have not gotten yet.

   Month      Day      Year             Full_Date
  January      1      1999             1999-01-01
  March       21      2019             2019-03-21

my code--though probably not the most efficient:

birth_data <- data.frame(
  Brirth_Month = separate(
    separate(play_data, col = "Birth", into = c("B_M_D", "B_Year"), sep = "\\,"),
    col = "B_M_D",
    into = c("B_Month", "B_Day"),
    sep = " ")$B_Month,
  Brirth_Day = as.numeric(separate(
    separate(play_data, col = "Birth", into = c("B_M_D", "B_Year"), sep = "\\,"),
    col = "B_M_D",
    into = c("B_Month", "B_Day"),
    sep = " ")$B_Day),
  Birth_Year = as.numeric(separate(play_data, col = "Birth", into = c("B_M_D", "B_Year"), sep = "\\,")$B_Year)
)

birth_data

edit original data example I made but not real date, just randomly made by me

play_data <- data.frame(
  Birth = c("Jan 23, 2019", "Feb 23, 1998", "June 3, 2003", "Oct 7, 2007", "Feb 28, 2004", "Apr 19, 2014", "Mar 11, 1988", "Sept 30, 2011")
)


str(playdate)
as.Date(dates$Birth)
Phil
  • 7,287
  • 3
  • 36
  • 66
Emm
  • 123
  • 8
  • Could you include some example data as it's read into R before you process it? See here ways to include data: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – admccurdy Sep 10 '20 at 19:55
  • 1
    sorry about that--I've put it up in the original post @admccurdy – Emm Sep 10 '20 at 20:06

1 Answers1

1

With lubridate this works:

library(lubridate)

play_data$Birth <- mdy(play_data$Birth)

admccurdy
  • 694
  • 3
  • 11