Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple yet confusing R question about bind_rows()

Tags:

r

dplyr

I'm trying to combine multiple dataframes into one dataframe using bind_rows. Each dataframe has the same column names and length. Let's say each dataframe is named "df" and there are 100 of them. So df1, df2, df3... df100.

I do not want to write each dataframe in the function as bind_rows(df1, df2 ... df100). I tried

total_df <- bind_rows(paste0(df1:df100))

It did not work. Would there be an easier way to do this? Thank you!!

like image 608
Anna Avatar asked Jun 25 '21 20:06

Anna


People also ask

What does bind_rows do in R?

bind_rows() function in R Programming is used to combine rows of two data frames.

What is the use of rbind?

Syntax of the rbind() function rbind(): The rbind or the row bind function is used to bind or combine the multiple group of rows together.

What is the difference between Cbind () and Rbind () functions?

cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows.

How does rbind works in R?

The name of the rbind R function stands for row-bind. The rbind function can be used to combine several vectors, matrices and/or data frames by rows. Above, you can find the basic code for rbind in R.

How to bind or combine rows in R?

The cbind () function combines R Objects By Rows Or Columns. To bind or combine rows in R, use the rbind () function. The rbind () stands for row binding. The rbind () function can combine various vectors, matrices, and/or data frames by rows. In short, to join two data frames (datasets) vertically, use the rbind () function.

How to use bind_rows () function in R programming?

bind_rows () function in R Programming is used to combine rows of two data frames. Here in the above code, we created 3 data frames data1, data2, data3 with rows and columns in it and then we use bind_rows () function to combine the rows that were present in the data frame.

What is rbind in R with example?

In other words, Rbind in R appends or combines vector, matrix or data frame by rows. bind_rows () function in dplyr package of R is also performs the row bind opearion. lets see an example of both the functions. In this Tutorial we will look at Example of row bind operation in R by using rbind () function.

How to bind data frame rows vertically in R?

rbind in R: How to Bind Data Frame Rows Vertically in R 1 rbind in R. ... 2 Binding two data frames of unequal length. ... 3 Binding vectors to the data frame using rbind () Create a data frame using vectors and then combine it with another vector using the rbind () function. 4 rbind fill – Row Bind with Missing Columns. ... 5 Conclusion. ...


1 Answers

The easier way would be to avoid having a bunch of dataframes named df1, ..., df100 from the start by making use of a list. Be that as it may. You could make use of lapply and get to put your df's into a list and call bind_rows on the list:

library(dplyr)

df1 <- mtcars
df2 <- mtcars
df3 <- mtcars

df_bind <- lapply(1:3, function(x) get(paste0("df", x))) %>% 
  bind_rows()

head(df_bind)
#>                        mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4...1         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag...2     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710...3        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive...4    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout...5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant...6           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

or using mget you could do

mget(paste0("df", 1:3)) %>% bind_rows()
like image 142
stefan Avatar answered Oct 21 '22 22:10

stefan