给定不同数据框的列表,映射并绑定不同数据框

时间:2018-11-05 10:52:23

标签: r functional-programming dplyr purrr

给出一个函数foo,该函数输出3个数据帧的列表:

structure(list(isSameUser = structure(1:2, .Label = c("0", "1"
), class = "factor"), n = c(212L, 72L), cum_n = c(212L, 284L), 
    user_id = c(1, 1)), .Names = c("isSameUser", "n", "cum_n", 
"user_id"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L))

structure(list(isSameUser = structure(1:2, .Label = c("0", "1"
), class = "factor"), n = c(54L, 18L), cum_n = c(54L, 72L), user_id = c(1, 
1)), .Names = c("isSameUser", "n", "cum_n", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L))

structure(list(error_abs_perc = 0.0694444444444444, user_id = 1), .Names = c("error_abs_perc", 
"user_id"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-1L))

我有1000个用户---->意味着我有1000 * 3个数据帧,第一个有1000个,第二个有1000个,第三个有1000个。 请告知如何map_dfr并获得3个绑定的数据帧。

我正在尝试使用imap_dfrmap_dfr来做到这一点。

对此进行了尝试: Use Dplyr::Bind_Rows and Purrr to Selectively Bind Different Dataframes In a List of Dataframes

但这并不能解决我的问题。

每个@markus请求:

list(structure(list(train_stats = structure(list(isSameUser = structure(1:2, .Label = c("0", 
"1"), class = "factor"), n = c(212L, 72L), cum_n = c(212L, 284L
), user_id = c(1L, 1L)), .Names = c("isSameUser", "n", "cum_n", 
"user_id"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L)), test_stats = structure(list(isSameUser = structure(1:2, .Label = c("0", 
"1"), class = "factor"), n = c(54L, 18L), cum_n = c(54L, 72L), 
    user_id = c(1L, 1L)), .Names = c("isSameUser", "n", "cum_n", 
"user_id"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-2L)), test_set_error = structure(list(error_abs_perc = 0.0694444444444444, 
    user_id = 1L), .Names = c("error_abs_perc", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))), .Names = c("train_stats", 
"test_stats", "test_set_error")), structure(list(train_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(238L, 46L), cum_n = c(238L, 284L), user_id = c(2L, 
    2L)), .Names = c("isSameUser", "n", "cum_n", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L)), test_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(60L, 12L), cum_n = c(60L, 72L), user_id = c(2L, 2L)), .Names = c("isSameUser", 
"n", "cum_n", "user_id"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L)), test_set_error = structure(list(error_abs_perc = 0.0555555555555556, 
    user_id = 2L), .Names = c("error_abs_perc", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))), .Names = c("train_stats", 
"test_stats", "test_set_error")), structure(list(train_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(232L, 52L), cum_n = c(232L, 284L), user_id = c(3L, 
    3L)), .Names = c("isSameUser", "n", "cum_n", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L)), test_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(58L, 14L), cum_n = c(58L, 72L), user_id = c(3L, 3L)), .Names = c("isSameUser", 
"n", "cum_n", "user_id"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L)), test_set_error = structure(list(error_abs_perc = 0.0138888888888889, 
    user_id = 3L), .Names = c("error_abs_perc", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))), .Names = c("train_stats", 
"test_stats", "test_set_error")), structure(list(train_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(224L, 60L), cum_n = c(224L, 284L), user_id = c(4L, 
    4L)), .Names = c("isSameUser", "n", "cum_n", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L)), test_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(56L, 16L), cum_n = c(56L, 72L), user_id = c(4L, 4L)), .Names = c("isSameUser", 
"n", "cum_n", "user_id"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L)), test_set_error = structure(list(error_abs_perc = 0.0694444444444444, 
    user_id = 4L), .Names = c("error_abs_perc", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))), .Names = c("train_stats", 
"test_stats", "test_set_error")), structure(list(train_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(232L, 52L), cum_n = c(232L, 284L), user_id = c(5L, 
    5L)), .Names = c("isSameUser", "n", "cum_n", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L)), test_stats = structure(list(
    isSameUser = structure(1:2, .Label = c("0", "1"), class = "factor"), 
    n = c(58L, 14L), cum_n = c(58L, 72L), user_id = c(5L, 5L)), .Names = c("isSameUser", 
"n", "cum_n", "user_id"), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L)), test_set_error = structure(list(error_abs_perc = 0.0138888888888889, 
    user_id = 5L), .Names = c("error_abs_perc", "user_id"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -1L))), .Names = c("train_stats", 
"test_stats", "test_set_error")))

1 个答案:

答案 0 :(得分:1)

我们可以graphics.h列出您的列表,然后使用transposemap

bind_rows