I have a data.frame with datetimes and values (between 0 and 1), and I would like to find the first occurrence of value = 1 per day .
df <- read.table(header = TRUE, text = '
Datetime Value
"2016-12-01 23:45:00" 0
"2016-12-01 23:50:00" 1
"2016-12-02 00:05:00" 1
"2016-12-02 00:10:00" 0
"2016-12-03 04:10:00" 0
"2016-12-03 04:15:00" 0
"2016-12-04 12:10:00" 1
"2016-12-04 12:15:00" 1
')
df$Datetime <- as.POSIXct(df$Datetime, "%Y-%m-%d %H:%M:%S", tz="UTC")
View(df)
I would like:
2016-12-01 23:50:00 1
2016-12-02 00:05:00 1
2016-12-04 12:10:00 1
I tried to solve the problem with match () and aggregate (), but so far no luck. Also, I was able to solve the for loop problem, but it was a) very slow and b) probably not the way it should be.
source
share