P.Rastogi P.Rastogi - 17 days ago 6
R Question

Subtracting 10 minutes each from a vector of times in R?

I have a column of times like this:

"2:43::00 PM"
"2:43:01 PM"
"2:43:05 PM"
"2:43:06 PM"
"2:43:07 PM"


I want to go to each of the row and subtract 10 minutes from each of the time such that the new column looks like this:

"2:33::00 PM"
"2:33:01 PM"
"2:33:05 PM"
"2:33:06 PM"
"2:33:07 PM"


I am using

difftime(strptime(data$time[i],format="%H:%M:%S"),strptime("00:10:00",format="%H:%M:%S"))


, but it is not showing correct results.

How can i approach this problem?

Answer

Try

format(strptime(str1, format = "%I:%M:%S %p") - 10*60, "%I:%M:%S %p")
#[1] "02:33:00 PM" "02:33:01 PM" "02:33:05 PM" "02:33:06 PM" "02:33:07 PM"

data

str1 <- c("2:43:00 PM", "2:43:01 PM", "2:43:05 PM", "2:43:06 PM", "2:43:07 PM")
Comments