Consider a tibble A with 2 columns of which column 1 contains time stamps (POSIXct class) and an Interval object b, which I have created using lubridate::int_diff, containing 9 individual time intervals.
Using dplyr, I would like to add 9 new columns to the tibble A, indicating whether the time stamp of each row falls within any of the intervals. Put differently, I would like to use the function %within% and distribute the vector output of length 9 across the 9 new columns.
What is the most effective using the dplyr package?
Example:
library(lubridate)
library(dplyr)
A <- tibble(Ts = ymd_hms(c("2018-01-01 15:12:04",
"2018-01-02 00:14:06","2018-01-05 12:00:00")),
P = c(1:3))
ts.start <- ymd_hms("2018-01-01 15:00:00")
ts.end <- ymd_hms("2018-01-02 15:30:00")
ts <- c(ts.start,sort(ts.end -
minutes(cumsum(c(15,15,30,30,60,60,60,60)))),ts.end)
b <- int_diff(ts)
# Applying %within" to the first element works
(A[[1,1]] %within% b) + 0
# The line with error.
mutate(A,New = Ts %within% b )
The last line produces an error as expected and would like to know how can define new variables based on applying a function with vector output on a variable column.