我正在练习使用purrr
包,但仍然不够。我尝试使用purrr
的map()
函数来迭代数据框列表,并选择特定列来创建新的数据框。我尝试了以下代码,但似乎不起作用:
library(tidyverse)
df1 <- data.frame(x = 1:5, col1 = letters[1:5])
df2 <- data.frame(x = 5:10, col2 = letters[5:10])
df3 <- data.frame(x = 10:15, col3 = letters[10:15])
list_dataframes <- list(df1, df2, df3)
# would like to do something like this
new_dataframe <- map_df(list_dataframes, ~select(., c("col1", "col2", "col3")))
#> Error in `map()`:
#> i In index: 1.
#> Caused by error in `select()`:
#> ! Can't subset columns that don't exist.
#> x Column `col2` doesn't exist.
#> Backtrace:
#> x
#> 1. +-purrr::map_df(list_dataframes, ~select(., c("col1", "col2", "col3")))
#> 2. | \-purrr::map(.x, .f, ...)
#> 3. | \-purrr:::map_("list", .x, .f, ..., .progress = .progress)
#> 4. | +-purrr:::with_indexed_errors(...)
#> 5. | | \-base::withCallingHandlers(...)
#> 6. | +-purrr:::call_with_cleanup(...)
#> 7. | \-global .f(.x[[i]], ...)
#> 8. | +-dplyr::select(., c("col1", "col2", "col3"))
#> 9. | \-dplyr:::select.data.frame(., c("col1", "col2", "col3"))
#> 10. | \-tidyselect::eval_select(expr(c(...)), data = .data, error_call = error_call)
#> 11. | \-tidyselect:::eval_select_impl(...)
#> 12. | +-tidyselect:::with_subscript_errors(...)
#> 13. | | \-rlang::try_fetch(...)
#> 14. | | \-base::withCallingHandlers(...)
#> 15. | \-tidyselect:::vars_select_eval(...)
#> 16. | \-tidyselect:::walk_data_tree(expr, data_mask, context_mask)
#> 17. | \-tidyselect:::eval_c(expr, data_mask, context_mask)
#> 18. | \-tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#> 19. | \-tidyselect:::walk_data_tree(new, data_mask, context_mask)
#> 20. | \-tidyselect:::eval_c(expr, data_mask, context_mask)
#> 21. | \-tidyselect:::reduce_sels(node, data_mask, context_mask, init = init)
#> 22. | \-tidyselect:::walk_data_tree(new, data_mask, context_mask)
#> 23. | \-tidyselect:::as_indices_sel_impl(...)
#> 24. | \-tidyselect:::as_indices_impl(...)
#> 25. | \-tidyselect:::chr_as_locations(x, vars, call = call, arg = arg)
#> 26. | \-vctrs::vec_as_location(...)
#> 27. \-vctrs (local) `<fn>`()
#> 28. \-vctrs:::stop_subscript_oob(...)
#> 29. \-vctrs:::stop_subscript(...)
#> 30. \-rlang::abort(...)
创建于2023年3月1日,使用reprex v2.0.2
LE:map_df(list_dataframes, ~select_if(., is.character))
可以工作,但不能正确绑定列。
任何帮助或见解将不胜感激!
2条答案
按热度按时间kyxcudwk1#
您可以像这样使用
map_df
(按行绑定或者,如果您的行在列表中具有完全相同的长度,那么您可以使用
bind_cols()
,(我将df1更改为df1 <- data.frame(x = 1:6, col1 = letters[1:6])
)btqmn9zl2#
我认为问题在于名称“col1”(或col2或col3)并不存在于所有data.tables中。