I have a list of dataframes that all have identical numbers of columns (and rows). I want to bind them using purrr::map_df
.
I try map_df(my_list)
and get
Error in as_mapper(.f, ...) : argument ".f" is missing, with no default
I'm not sure what's wrong with my list. It looks good to me (each dataframe has a unique name):
To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.
map() returns a list or a data frame; map_lgl() , map_int() , map_dbl() and map_chr() return vectors of the corresponding type (or die trying); map_df() returns a data frame by row-binding the individual elements.
Bind together two data frames by their rows or columns in R, To join two data frames by their rows, use the bind_rows() function from the dplyr package in R. Similarly, you may use dplyr's bind_cols() function to join two data frames based on their columns.
In order to create a list of data frames in R, we can use the list() function. Within the function, we simply have to specify all data frames that we want to include to the list separated by a comma.
Note: Many purrr functions result in lists. In much of my work I prefer to work in data frames, so this post will focus on using purrr with data frames. Here’s what I mean. Imagine you have a function like this: myFunction <- function (arg1) { # Cool stuff in here that returns a data frame!
Let’s first create three data frames in R… …and then let’s store these data frames in a list: If we want to merge a list of data frames with Base R, we need to perform two steps. First, we need to create our own merging function. Note that we have to specify the column based on which we want to join our data within this function (i.e. “id”):
This is an efficient implementation of the common pattern of do.call (rbind, dfs) or do.call (cbind, dfs) for binding many data frames into one. ... Data frames to combine. Each argument can either be a data frame, a list that could be a data frame, or a list of data frames.
There’s a purrr function for that! Use map2_dfr () If you’re dealing with 2 or more arguments, make sure to read down to the Crossing Your Argument Vectors section. And if your function has 3 or more arguments, make a list of your variable vectors and use pmap_dfr ()
Try bind_rows(my_list)
.
map_df(.x, .f)
takes a list (the argument .x
) and applies a function (the argument .f
), and returns a dataframe. You've got the first argument right (probably), but you haven't supplied a function to map. If you want to use map
, you might try map_df(my_list, rbind)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With