How to optimize a function that matches observations according to certain criteria
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~