r + dplyr filtering out time series -


i have data looks @ group of people , fruits eat on time. want use dplyr @ each individual person until eat banana , summarise fruits ate up until eat first banana.

data:

data <-  structure(list(user = c(1234l, 1234l, 1234l, 1234l, 1234l, 1234l,      1234l, 1234l, 1234l, 1234l, 1234l, 1234l, 9584l, 9584l, 9584l,      9584l, 9584l, 9584l, 9584l, 9584l, 9584l, 4758l, 4758l, 4758l,      4758l, 4758l, 4758l), site = structure(c(1l, 6l, 1l, 1l, 6l,      5l, 5l, 3l, 4l, 1l, 2l, 6l, 1l, 6l, 5l, 5l, 3l, 2l, 6l, 6l, 6l,      4l, 2l, 5l, 5l, 4l, 2l), .label = c("apple", "banana", "lemon",      "lime", "orange", "pear"), class = "factor"), time = c(1l, 2l,      3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l, 11l, 12l, 1l, 2l, 3l, 4l, 5l,      6l, 7l, 8l, 9l, 5l, 6l, 7l, 8l, 9l, 10l), int = structure(c(2l,      2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 2l, 2l, 2l, 2l, 2l, 2l,      1l, 2l, 2l, 2l, 2l, 1l, 2l, 2l, 2l, 1l), .label = c("banana",      "other"), class = "factor")), .names = c("user", "site", "time",      "int"), row.names = c(na, -27l), class = "data.frame") 

my initial thought group data find first instance of each user eating banana:

data <- data %>% transform(var = ifelse(site=="banana", 'banana','other'))  data_ban <- data %>%      filter(var=='banana') %>%      group_by(user, var, time) %>%     group_by(user) %>%     summarise(first_banana = min(time)) 

but i'm stuck on how apply original "data" dataframe, , set filter says: each user, include data until time given in "data_ban". ideas?

you try slice

data %>%      group_by(user) %>%       slice(1:(which(int=='banana')[1l])) 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -