Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding together dataframes from a list in purrr

Tags:

r

purrr

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):

enter image description here

like image 345
Nazer Avatar asked Jan 26 '18 22:01

Nazer


People also ask

How do I combine a list of data in a Dataframe in R?

To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.

What does Map_df () do in R?

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.

How do you bind a Dataframe?

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.

How do I add a Dataframe to a list in R?

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.

Can I use Purrr with data frames?

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!

How to merge a list of data frames in base R?

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”):

How to bind multiple data frames into one in Python?

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.

How do I use Purrr in Python with multiple arguments?

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 ()


1 Answers

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).

like image 107
twedl Avatar answered Oct 07 '22 16:10

twedl