Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # This is a function for dividing a time series into periods, with allowence for gaps (threshold)
  2. # data = dataset
  3. # variable = time for measurement, e.g. year (duplicates allowed)
  4. # threshold = number of consecutive years (e.g.) allowed missing in a period
  5. # filter_less_than = filters periods with less than N values (not the length from min to max!)
  6.  
  7. periods <- function(data,
  8. variable,
  9. threshold = 3,
  10. filter_less_than = NA){
  11. variable_enquo <- enquo(variable)
  12. groups_vec <- data %>% group_vars() %>% syms
  13. out <- data %>%
  14. dplyr::arrange(!!variable_enquo) %>%
  15. mutate(period = cumsum(c(1, diff(!!variable_enquo) >= threshold+1))) %>%
  16. group_by(period, add=T) %>%
  17. mutate(n_year_period = n_distinct(!!variable_enquo))
  18.  
  19. if(!is_empty(groups_vec)){out <- out %>% group_by(!!!groups_vec)}else{out <- out %>% ungroup}
  20.  
  21. if(!is.na(filter_less_than)){out <- out %>%
  22. filter(n_year_period >= filter_less_than) %>%
  23. mutate(period = cumsum(c(1, diff(!!variable_enquo) >= threshold+1)))
  24. }
  25.  
  26. return(out)
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement