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)