Search code examples
rpurrrfurrr

mapping over lists and creating a train and test split


I have a list of data frames and I would like to split each of the lists into a training and testing set.

What I am trying is the following:

library(furrr)
library(purrr)
library(dplyr)
train <- df %>% 
  future_map(., ~as_tibble(.) %>% 
               sample_frac(0.70))

test <- df %>% 
  future_map2(., ~anti_join(df, train, by = "ID"))

I am getting an error on the test part of the code.

The error is telling me that:

argument ".f" is missing, with no default

The function .f I want to apply is the anti_join function.

Data:

df <- list(structure(list(ID = c(32854L, 87264L, 10046L, 19734L, 46776L, 
69104L), Var1 = c(0.422291041272086, 0.421857549178741, 0.833626138132112, 
0.0548106066414176, 0.624276309555977, 0.71510623528032), Var2 = c(12.1523922938606, 
11.8078760576117, 15.0796093186172, 14.6689943885814, 16.2489940803754, 
12.6618473977339)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(ID = c(64813L, 28351L, 
98206L, 11662L, 39650L, 48688L), Var1 = c(0.7736786, 0.9184715, 
0.6961003, 0.904335, 0.2900523, 0.5886406), Var2 = c(14.9093, 
14.37623, 13.50509, 13.84473, 12.35784, 14.02979)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    ID = c(34745L, 20879L, 16576L, 23177L, 64122L, 23805L), Var1 = c(0.4781384, 
    0.89258, 1.993566, 0.291347, 0.1067107, 0.866149), Var2 = c(12.96561, 
    12.33563, 11.8721, 14.79747, 13.08436, 14.70748)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    ID = c(42108L, 11726L, 78484L, 36898L, 60224L, 61154L), Var1 = c(0.877778213319744, 
    1.33963228732897, 0.806292976614067, 0.972259512214242, NA, 
    0.756496381825957), Var2 = c(11.5246035772182, 11.6156242530741, 
    12.9551613288682, 13.1341296830114, NA, 13.9791350710985)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame")))

Solution

  • Since you're using future_map2(), you need both a .x and .y argument, which you feed into the anti_join() function.

    Your example was missing train, so future_map2() thought that anti_join() was your .y argument, and so it thought that .f was missing, hence the error.

    test <- df %>% 
      future_map2(.x = ., 
                  .y = train, 
                  .f = ~anti_join(.x, .y, by = "ID"))