R/find_neighbors.R

Defines functions find_neighbors_idx

Documented in find_neighbors_idx

#' A point n2 is a neighbor of point n1 with respect to param p if the two points
#' n1 and n2 are identical everywhere except for param p. 
#' @param df The data.frame containing all known combinations
#' @param param_vals The combination for which we want to find neighbors
#' @param param_name Name of the parameter (dimension) along which to search for neighbors.
find_neighbors_idx <- function(df, paramset, param_name){
  
  without_param_vals <- 
    paramset[!(names(paramset) == param_name)]
  
  which(
    apply(df[, names(without_param_vals)], 1, 
          FUN = function(v) all(v == without_param_vals))
  )
  
}
# 
# 
# df = tribble(
#   ~x1, ~x2, ~x3,
#   1  ,   1,   1,
#   1  ,   1,   2,
#   1  ,   1,   3,
#   2  ,   1,   1,
#   2  ,   1,   2,
#   2  ,   1,   3,
#   3  ,   1,   1,
#   3  ,   1,   2,
#   3  ,   1,   3
# )
# 
# param_vals <- c(x1 = 2, x2 = 1, x3 = 2)
# 
# param_name <- 'x1'
# 
# find_neighbors(df, param_vals, param_name)
artichaud1/tidygrid documentation built on July 6, 2018, 9:10 a.m.