I am attempting to add together two "timestamps" in R. I say timestamps in quotations because they are character variables and are not technically times. The data in the ExtractionTime
column is the minute/second point of a video and the data in the PropertyTime
column is the timestamp at the point of the video that needs to be cut. I ideally want to add ExtractionTime
and PropertyTime
and then return their sum in the ClipTime
column. I have created code that does just this but it does not add minutes once the code hits 59 seconds. Any idea of how to do this? Thanks!
Here is some of my data:
ExtractionTime
<chr>
PropertyTime
<chr>
ClipTime
<lgl>
00:16:49 10:00:13 NA
00:16:50 10:00:13 NA
00:16:51 10:00:13 NA
00:16:52 10:00:13 NA
00:16:53 10:00:13 NA
00:16:54 10:00:13 NA
Here is my code:
time.combine=function(x, y)
for (i in seq_len(length(x))) {
first.ex<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\1", x))
second.ex<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\2", x))
third.ex<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\3", x))
first.prop<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\1", y))
second.prop<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\2", y))
third.prop<-as.numeric(sub("(\\d+):(\\d+):(\\d+)", "\\3", y))
first<-first.ex+first.prop
second<-second.ex+second.prop
third<-third.ex+third.prop
combined.times<-paste(first,second,third, sep = ":")
return(combined.times)
}
test.df$ClipTime<-time.combine(test.df$ExtractionTime, test.df$PropertyTime)
Which results in...
ExtractionTime
<chr>
PropertyTime
<chr>
ClipTime
<chr>
00:16:49 10:00:13 10:16:62
00:16:50 10:00:13 10:16:63
00:16:51 10:00:13 10:16:64
00:16:52 10:00:13 10:16:65
00:16:53 10:00:13 10:16:66
00:16:54 10:00:13 10:16:67
But what I want is...
ExtractionTime
<chr>
PropertyTime
<chr>
ClipTime
<chr>
00:16:49 10:00:13 10:17:02
00:16:50 10:00:13 10:17:03
00:16:51 10:00:13 10:17:04
00:16:52 10:00:13 10:17:05
00:16:53 10:00:13 10:17:06
00:16:54 10:00:13 10:17:07
How can I get this data instead?