Reputation: 4040
Given a function foo
that outputs a list of 3 dataframes:
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))
I have 1000 users ----> meaning I have 1000*3 dataframes, 1000 of the first, 1000 of the second and 1000 of the third.
Please advise how can I map_dfr
it and get 3 binded dataframes.
I am trying to do this using imap_dfr
and map_dfr
.
Tried this: Use Dplyr::Bind_Rows and Purrr to Selectively Bind Different Dataframes In a List of Dataframes
But it doesn't solve my issue.
Per @markus request:
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")))
Upvotes: 1
Views: 81
Reputation: 26343
We can transpose
your list and then use map
and bind_rows
library(purrr)
map(transpose(lst), bind_rows)
#$train_stats
# A tibble: 10 x 4
# isSameUser n cum_n user_id
# <fct> <int> <int> <int>
# 1 0 212 212 1
# 2 1 72 284 1
# 3 0 238 238 2
# 4 1 46 284 2
# 5 0 232 232 3
# 6 1 52 284 3
# 7 0 224 224 4
# 8 1 60 284 4
# 9 0 232 232 5
#10 1 52 284 5
#$test_stats
# A tibble: 10 x 4
# isSameUser n cum_n user_id
# <fct> <int> <int> <int>
# 1 0 54 54 1
# 2 1 18 72 1
# 3 0 60 60 2
# 4 1 12 72 2
# 5 0 58 58 3
# 6 1 14 72 3
# 7 0 56 56 4
# 8 1 16 72 4
# 9 0 58 58 5
#10 1 14 72 5
#$test_set_error
# A tibble: 5 x 2
# error_abs_perc user_id
# <dbl> <int>
#1 0.0694 1
#2 0.0556 2
#3 0.0139 3
#4 0.0694 4
#5 0.0139 5
Upvotes: 1