Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a function for dividing a time series into periods, with allowence for gaps (threshold)
- # data = dataset
- # variable = time for measurement, e.g. year (duplicates allowed)
- # threshold = number of consecutive years (e.g.) allowed missing in a period
- # filter_less_than = filters periods with less than N values (not the length from min to max!)
- periods <- function(data,
- variable,
- threshold = 3,
- filter_less_than = NA){
- variable_enquo <- enquo(variable)
- groups_vec <- data %>% group_vars() %>% syms
- out <- data %>%
- dplyr::arrange(!!variable_enquo) %>%
- mutate(period = cumsum(c(1, diff(!!variable_enquo) >= threshold+1))) %>%
- group_by(period, add=T) %>%
- mutate(n_year_period = n_distinct(!!variable_enquo))
- if(!is_empty(groups_vec)){out <- out %>% group_by(!!!groups_vec)}else{out <- out %>% ungroup}
- if(!is.na(filter_less_than)){out <- out %>%
- filter(n_year_period >= filter_less_than) %>%
- mutate(period = cumsum(c(1, diff(!!variable_enquo) >= threshold+1)))
- }
- return(out)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement