R/clust_by_port.R

Defines functions clust_by_port

Documented in clust_by_port

#' Cluster values by port

#' Function to cluster values by port. Can specify a cut_point or use a default calculation
#' from Trevor's fishing opportunity paper

#' @param port Name or index for port of interest
#' @param cut_point Default is calculation based on median tow distance

#' @export

#Write cluster function for mclapply call
clust_by_port <- function(port, cut_point = NA, input){

  temp <- input %>% filter(dport_desc == port)

  #Filter out unique tows
  unq_tows <- temp %>% distinct(haul_id, .keep_all = T)
  # unq_tows <- temp %>% group_by(haul_id) %>% filter(row_number(haul_id) == 1) %>% as.data.frame
  xx <- clust_tows(dat = unq_tows)

  if(is.na(cut_point)) calc_cut <- sqrt(2) * (median(temp$dist_slc_km, na.rm = T) * 360) / 40075
  if(is.na(cut_point) == FALSE) calc_cut <- cut_point

  unq_tows <- cut_for_merge(input_data = unq_tows, clust_input = xx, cut_point = calc_cut)

  temp <- left_join(temp, unq_tows, by = 'haul_id')
  return(temp)
}
peterkuriyama/ch4 documentation built on June 18, 2021, 9:59 a.m.