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:
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.
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.
The only problem is there are a couple of weird observations that I want to treat as NA that qualtrics downloads into my dataset for the first two rows. The values are: {"ImportId":"recordedDate","timeZone":"America/New_York"} & Recorded Date
So my actual dataset I guess is more like:
Also, I am realizing this code separates in a display table, but doesn't seem store the variables in my dataset, so if I resave my dataset, the "date" and "time" variables were not there.
I still appreciate this code though. It was useful to know, but doesn't fully resolve my problem.
The second row in fact holds valuable information; it specifies the time zone that the timestamps are in.
Given this, I would suggest first converting it into a datetime object with the proper time zone and then doing the rest. That will also have the desired side effect of converting the first two observations into NA values since they do not follow the specified date format.
dplyr does not modify in-place so you need to assign the transformed data frame to a variable to store the results. I did not do this in the previous post. Please see below.
Note: I'm not using as_date() and as_hms() here so the resulting columns are just character vectors. If you need them as date and hms objects (useful if you're doing date and time-specific arithmetic), you can convert them using the mutate() call from my last post.
Thank you so much! This was great! I am new to R, so I appreciate you spending the extra time showing me how to do this. Not only is this useful for this problem, but helps me understand some of the process/packages that will be useful in the future! Thanks again