Thomas Speidel - 1 year ago 60
R Question

# Expand Time Into Minutes Increments

I'm trying to expand time into minute increments from

`tstart`
to
`tstop`
:

``````## Make reproducible example
> dput(df)
structure(list(id = c(101, 101, 101, 101, 101, 101, 101, 104,
104, 104, 104, 104, 104, 104, 104), tstart = structure(c(1427191679,
1427278247, 1427656267, 1427668088, 1427710073, 1427710123, 1427717538,
1429282892, 1429923963, 1430137643, 1430270378, 1430270408, 1430430281,
1431435022, 1431691603), class = c("POSIXct", "POSIXt"), tzone = ""),
tstop = structure(c(1427192645, 1427279046, 1427656779, 1427668511,
1427710123, 1427710556, 1427718113, 1429283349, 1429924393,
1430138070, 1430270408, 1430270857, 1430430872, 1431435865,
1431692517), class = c("POSIXct", "POSIXt")), event.seq = c(10,
11, 12, 13, 14, 14, 15, 10, 11, 12, 13, 13, 14, 15, 16)), class = "data.frame", row.names = c(NA,
-15L), .Names = c("id", "tstart", "tstop", "event.seq"))

## Expand into multiple rows of 1 min increments start=tstart stop=tstop
df <- df %>%
group_by(id, event.seq) %>%
summarize(tstart = min(tstart), tstop = max(tstop)) %>%
do(data.frame(minutes = seq(.\$tstart, .\$tstop, by = "1 min")))
``````

This results in the error:

``````Error in seq.POSIXt(.\$tstart, .\$tstop, by = "1 min") :
'from' must be of length 1
``````

which I suspect has something to do with the
`seq.POSIXt`
call.
Unfortunately, I wasn't able to solve this.

The reason is that after the `summarise` step, `group_by` effect is gone. One way is to add `rowwise()` before the `do`.

``````res<- df %>%
group_by(id, event.seq) %>%
summarize(tstart = min(tstart), tstop = max(tstop))  %>%
rowwise() %>%
do(data.frame(., minutes = seq(.\$tstart, .\$tstop, by = "1 min")))
str(res)
#Classes ‘rowwise_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 140 obs. of  5 variables:
#\$ id       : num  101 101 101 101 101 101 101 101 101 101 ...
#\$ event.seq: num  10 10 10 10 10 10 10 10 10 10 ...
#\$ tstart   : POSIXct, format: "2015-03-24 15:37:59" "2015-03-24 15:37:59" "2015-03-24 15:37:59" "2015-03-24 15:37:59" ...
#\$ tstop    : POSIXct, format: "2015-03-24 15:54:05" "2015-03-24 15:54:05" "2015-03-24 15:54:05" "2015-03-24 15:54:05" ...
#\$ minutes  : POSIXct, format: "2015-03-24 15:37:59" "2015-03-24 15:38:59" "2015-03-24 15:39:59" "2015-03-24 15:40:59" ...
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download