The function to cut - who could think of that? ;) And it needs just two arguments:
x - your vector/variable to cut, and
breaks - indication where to cut.
Let’s say you record your cat for 30 min (it can be dog, for dog-lovers). Then based on the recording, every second you establish the cat’s mood. Now you want to analize how much time on average, within a 1 min time-window, your cat is in one of the three moods (after all you are a scientist…).
Your data frame may look like this:
set.seed(453)
TimeInterval <- seq(1,1800,1) # duration of 30 min recording in seconds, with 1 sec accuracy
Mood <- sample(c("happy", "bored", "undecided") , 1800, replace = TRUE) # a state of mood represented in 3 levels: happy, bored and undecided
catmood <- data.frame(TimeInterval, Mood)
And with cut() and dplyr help it is easy-peasy to calculate the mean amount of time per 1 minute when your kitty is happy, deadly bored or simply undecided.
library(dplyr) # in the flow with marvellous pipe operator ;)
catmood %>% # take you data frame, then
mutate(TimeBin = cut(TimeInterval, breaks = seq(0, 1800,60))) %>%
# create a new variable that cuts your time-line into desired time bins,
# so 60 seconds (60 sec = 1 min, by now you should be familiar with the clock;),
# [note that in breaks argument in cut() you create points to cut: 60,120,180, etc)],
# then
group_by(TimeBin, Mood) %>%
# group by the bins and mood, then
summarize(nsecsInmood = n()) %>%
# count the number of cases when each mood type appeared within given time-bin
# (i.e. amout of time the cat was in one of the three mood type), then
group_by(Mood) %>%
# group again by mood,to finally calculate
# mean amount of the time during 1 minute of recording,
# when your cat was in one of the mood types
summarize(Meam_nsecsInmood = mean(nsecsInmood))
## # A tibble: 3 x 2
## Mood Meam_nsecsInmood
## <fct> <dbl>
## 1 bored 19.7
## 2 happy 18.8
## 3 undecided 21.4
Fussy bastard ;) but at least pretty balanced in terms mood diversity