mardi 29 mai 2018

Add time increment, based on ifelse statement/ another column, to POSIXct

My dataset includes time-stamped events that occur during rounds of team-sport matches. Each round 5 minutes in duration. I wish to add the cumulative time that each time stamp occurs however, I only have the time an event occurs within a specified time.

An example of my dataset is:

 > head(OutcomeData$Match_EndingRound, 15)
 [1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2
> head(OutcomeData$TimeStamp, 15)
 [1] "1899-12-31 00:02:36 UTC" "1899-12-31 00:02:36 UTC" "1899-12-31 00:03:01 UTC" "1899-12-31 00:03:01 UTC" "1899-12-31 00:03:21 UTC"
 [6] "1899-12-31 00:03:21 UTC" "1899-12-31 00:04:37 UTC" "1899-12-31 00:04:37 UTC" "1899-12-31 00:02:19 UTC" "1899-12-31 00:02:19 UTC"
[11] "1899-12-31 00:02:19 UTC" "1899-12-31 00:02:19 UTC" "1899-12-31 00:02:09 UTC" "1899-12-31 00:02:09 UTC" "1899-12-31 00:02:09 UTC"

dput(head(OutcomeData$TimeStamp, 15))
structure(c(-2209075044, -2209075044, -2209075019, -2209075019, 
-2209074999, -2209074999, -2209074923, -2209074923, -2209075061, 
-2209075061, -2209075061, -2209075061, -2209075071, -2209075071, 
-2209075071), class = c("POSIXct", "POSIXt"), tzone = "UTC")

I wish to add 5 minutes to the time-stamped data, for every round = 2. When rounds are equal to 3, 4 and 5 (not displayed in my example), I wish to add 10, 15 and 20 minutes, respectively.

I have attempted the following:

OutcomeData$CumulativeTime <- ifelse(OutcomeData$Match_EndingRound==1, OutcomeData$TimeStamp,
                              ifelse(OutcomeData$Match_EndingRound==2, OutcomeData$TimeStamp + 300,
                              ifelse(OutcomeData$Match_EndingRound==3, OutcomeData$TimeStamp,
                              ifelse(OutcomeData$Match_EndingRound==4, OutcomeData$TimeStamp,
                              ifelse(OutcomeData$Match_EndingRound==5, OutcomeData$TimeStamp + 1200, OutcomeData$TimeStamp)))))

Although I ended up with this output and unsure where I have gone wrong?

 head(OutcomeData$CumulativeTime, 15)
 [1] -2209075044 -2209075044 -2209075019 -2209075019 -2209074999 -2209074999 -2209074923 -2209074923 -2209074761 -2209074761 -2209074761 -2209074761
[13] -2209074771 -2209074771 -2209074771

My anticipated output would be:

> head(OutcomeData$TimeStamp, 15)
     [1] "1899-12-31 00:02:36 UTC" "1899-12-31 00:02:36 UTC" "1899-12-31 00:03:01 UTC" "1899-12-31 00:03:01 UTC" "1899-12-31 00:03:21 UTC"
     [6] "1899-12-31 00:03:21 UTC" "1899-12-31 00:04:37 UTC" "1899-12-31 00:09:37 UTC" "1899-12-31 00:07:19 UTC" "1899-12-31 00:07:19 UTC"
    [11] "1899-12-31 00:07:19 UTC" "1899-12-31 00:07:19 UTC" "1899-12-31 00:07:09 UTC" "1899-12-31 00:07:09 UTC" "1899-12-31 00:07:09 UTC"

Any assistance would be greatly appreciated. Thank you!

Aucun commentaire:

Enregistrer un commentaire