I'm a PhD student (not that experienced in R), and I'm trying to recode a string variable, called RecordedDate into two separate variables: a Date variable (stored in a standard date format) and a Time variable (stored in a standard time format...preferably). My dataframe is called edeq2.1.
An example my observation format for this variable is: 7/28/2018 6:43. See below for example dataset:
RecordedDate = c("8/6/2018 18:56", "7/26/2018 10:43", "7/28/2018 6:43", "8/4/2018 8:36")
edeq2.1<- data.frame(RecordedDate)
I tried two methods, as suggested to me:
- I tried using the
anytime package, as suggested to me, but when for values where the value part of the time is one digit (such as the 7/28/2018 6:43" entry), the time part of the variable is not coded correctly (recoded value = 2018-07-28 00:00). Adding a useR=TRUE argument didn't help.
This was the code:
RecordedDate = c("8/6/2018 18:56", "7/26/2018 10:43", "7/28/2018 6:43", "8/4/2018 8:36")
edeq2.1<- data.frame(RecordedDate)
edeq2.1$RecordedDate_recoded <- anytime::anytime(edeq2.1$RecordedDate))
edeq2.1$Time<- format(edeq2.1$pt, "%H:%M" )
edeq2.1$Date<- format(edeq2.1$pt, "%Y-%m-%d")
- I also tried the split string function,
strsplit:
edeq2.1$Recoded_split=strsplit(edeq2.1$RecordedDate, " ")
But I'm unsure of how to rename and save the split values after that into a Date and Time variable. Additionally, I could probably convert the Date variable into standard time, but would have trouble doing this for the Time variable because of the nonstandard formatting. Any suggestions to fix my problem would be greatly appreciated! Even a suggestion that helps me separate and name the date and time variable successfully (without reformatting) would still be helpful.