Home > Software engineering >  How to use a vector's values as arguments in cbind?
How to use a vector's values as arguments in cbind?

Time:11-14

I have many data frames that I would like to join using cbind. The names of the data frames are stored in a vector. How to do this without having to type the names of all of the data frames as arguments for cbind?

For example, after

df1<-mtcars[1:10,1:2]

df2<-mtcars[1:10,3:4]

df3<-mtcars[1:10,5:6]

my_data_frames<-c("df1","df2","df3")

then

cbind(get(my_data_frames))

does not give the same output as

cbind(df1,df2,df3)

CodePudding user response:

This is one of the reasons why it's good practice to keep related data frames in a list: it stops you having to pull them all together from the global environment.

The easiest way to get your data frames bound together is to first get them into a list. Since get only accepts length one arguments, you need to use mget to get them in a list. cbind won't accept a list directly, so we have to pass all the members of the list as parameters to cbind using do.call:

do.call(cbind, mget(my_data_frames))
#>                    mpg cyl  disp  hp drat    wt
#> Mazda RX4         21.0   6 160.0 110 3.90 2.620
#> Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875
#> Datsun 710        22.8   4 108.0  93 3.85 2.320
#> Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215
#> Hornet Sportabout 18.7   8 360.0 175 3.15 3.440
#> Valiant           18.1   6 225.0 105 2.76 3.460
#> Duster 360        14.3   8 360.0 245 3.21 3.570
#> Merc 240D         24.4   4 146.7  62 3.69 3.190
#> Merc 230          22.8   4 140.8  95 3.92 3.150
#> Merc 280          19.2   6 167.6 123 3.92 3.440

Created on 2021-11-13 by the reprex package (v2.0.0)

CodePudding user response:

One approach is to use the apply family with get. Specifically lapply takes care of putting data into lists which then can be stored in a data frame.

data.frame( lapply( my_data_frames, get ) )

                   mpg cyl  disp  hp drat    wt
Mazda RX4         21.0   6 160.0 110 3.90 2.620
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875
Datsun 710        22.8   4 108.0  93 3.85 2.320
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440
Valiant           18.1   6 225.0 105 2.76 3.460
Duster 360        14.3   8 360.0 245 3.21 3.570
Merc 240D         24.4   4 146.7  62 3.69 3.190
Merc 230          22.8   4 140.8  95 3.92 3.150
Merc 280          19.2   6 167.6 123 3.92 3.440

CodePudding user response:

Using mget and dplyr::bind_cols -

result <- dplyr::bind_cols(mget(my_data_frames))
result

#                   mpg cyl  disp  hp drat    wt
#Mazda RX4         21.0   6 160.0 110 3.90 2.620
#Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875
#Datsun 710        22.8   4 108.0  93 3.85 2.320
#Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215
#Hornet Sportabout 18.7   8 360.0 175 3.15 3.440
#Valiant           18.1   6 225.0 105 2.76 3.460
#Duster 360        14.3   8 360.0 245 3.21 3.570
#Merc 240D         24.4   4 146.7  62 3.69 3.190
#Merc 230          22.8   4 140.8  95 3.92 3.150
#Merc 280          19.2   6 167.6 123 3.92 3.440
  •  Tags:  
  • r
  • Related