Recode "date & time variable" into two separate variables

281 Views Asked by At

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 and a Time variable. I am using RStudio.

An example of values are:

8/6/2018 18:56
7/26/2018 10:43
7/28/2018 8:36  

I would like to you the first part of the value (example: 08/6/2018) to reformat this into a date variable, and the second part of the value (example: 18:56) into a time variable.

  1. I'm thinking the first step would be to create code that can break this up into two variables, based on some rule. I’m thinking maybe I can separate separate everything before the "space" into the Date variable, and after the "space" in the Time variable. I am not able to figure this out.

  2. Then, I'm looking for code that would change the Date from a "string" variable to a "date" type variable. I’m not sure if this is correct, but I’m thinking something like: better_date <- as.Date(Date, "%m/%d/%Y")

  3. Finally, then I would like to change theTime variable to a "time" type format (if this exists). Not sure how to do this part either, but something that indicates hours and minutes. This part is less important than getting the date variable.

1

There are 1 best solutions below

6
Dirk is no longer here On BEST ANSWER

Two immediate ways:

  1. strsplit() on the white space

  2. The proper ways: parse, and then format back out.

Only 2. will guarantee you do not end up with hour 27 or minute 83 ...

Examples:

R> data <- c("8/6/2018 18:56", "7/26/2018 10:43", "7/28/2018 8:36")
R> strsplit(data, " ")
[[1]]
[1] "8/6/2018" "18:56"   

[[2]]
[1] "7/26/2018" "10:43"    

[[3]]
[1] "7/28/2018" "8:36"     

R> 

And:

R> data <- c("8/6/2018 18:56", "7/26/2018 10:43", "7/28/2018 8:36")
R> df <- data.frame(data)
R> df$pt <- anytime::anytime(df$data)   ## anytime package used
R> df$time <- format(df$pt, "%H:%M")
R> df$day <- format(df$pt, "%Y-%m-%d")
R> df
             data                  pt  time        day
1  8/6/2018 18:56 2018-08-06 18:56:00 18:56 2018-08-06
2 7/26/2018 10:43 2018-07-26 10:43:00 10:43 2018-07-26
3  7/28/2018 8:36 2018-07-28 00:00:00 00:00 2018-07-28
R> 

I often collect data in a data.frame (or data.table) and then add column by column.