Question

I have a data frame 'rta' with two date variables "Date.Time of Accident" and "Date.Time. of Death". The variable 'Date.Time.Death' has the date and time separated by a space, with time in 24 hr format but not uniform in having minutes. Similarly the other variable Date.Time.Accident too is in this format only.

rta <- data.frame(DMYT.Death = c('2008-12-30 10.15','2008-12-15 23','2008-12-15 18.15','2008-12-26 17','2008-12-21 14.45','2008-12-19 23'),stringsAsFactors=F)

I want to make this variable into a uniform format of YYYY-MM-DD hh:mm, for example 2008-12-30 10:15 which can be used to calculate period of survival from both the date variables . How to get it this way?

Was it helpful?

Solution

rta
##            datevar
## 1 2008-12-30 10.15
## 2    2008-12-15 23
## 3 2008-12-15 18.15
## 4    2008-12-26 17
## 5 2008-12-21 14.45
## 6    2008-12-19 23


# First we add the 'minutes' to strings where they are missing.


rta$datevar[!grepl(".*\\..*", rta$datevar)] <- paste0(rta$datevar[!grepl(".*\\..*", rta$datevar)], ".00")
rta
##            datevar
## 1 2008-12-30 10.15
## 2 2008-12-15 23.00
## 3 2008-12-15 18.15
## 4 2008-12-26 17.00
## 5 2008-12-21 14.45
## 6 2008-12-19 23.00


# Convert the datevar to POSIXct class.
rta$datevar <- as.POSIXct(rta$datevar, format = "%Y-%m-%d %H.%M")
rta
##               datevar
## 1 2008-12-30 10:15:00
## 2 2008-12-15 23:00:00
## 3 2008-12-15 18:15:00
## 4 2008-12-26 17:00:00
## 5 2008-12-21 14:45:00
## 6 2008-12-19 23:00:00
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top