映射列表并创建训练和测试拆分
mapping over lists and creating a train and test split
我有一个数据框列表,我想将每个列表拆分成一个训练集和测试集。
我正在尝试的是以下内容:
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"))
我在代码的 test
部分遇到错误。
错误告诉我:
argument ".f" is missing, with no default
我要申请的函数.f
是anti_join
函数
数据:
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")))
由于您使用的是 future_map2()
,因此您需要 .x
和 .y
参数,并将其输入到 anti_join()
函数中。
您的示例缺少 train
,因此 future_map2()
认为 anti_join()
是您的 .y
参数,因此它认为缺少 .f
,因此错误。
test <- df %>%
future_map2(.x = .,
.y = train,
.f = ~anti_join(.x, .y, by = "ID"))
我有一个数据框列表,我想将每个列表拆分成一个训练集和测试集。
我正在尝试的是以下内容:
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"))
我在代码的 test
部分遇到错误。
错误告诉我:
argument ".f" is missing, with no default
我要申请的函数.f
是anti_join
函数
数据:
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")))
由于您使用的是 future_map2()
,因此您需要 .x
和 .y
参数,并将其输入到 anti_join()
函数中。
您的示例缺少 train
,因此 future_map2()
认为 anti_join()
是您的 .y
参数,因此它认为缺少 .f
,因此错误。
test <- df %>%
future_map2(.x = .,
.y = train,
.f = ~anti_join(.x, .y, by = "ID"))