1

I have a list of data.frames in R. I want to join them together using join_all but the variable they have in common does not have exactly the same name.

Here is an example dataset

compA <- rep(1:35)
compB <- rep(1:35)
compC <- rep(1:35)
valuesa <- rnorm(35)
valuesb <- rnorm(35)
valuesc <- rnorm(35)
A <- data.frame(compA, valuesa)
B <- data.frame(compB, valuesb)
C <- data.frame(compC, valuesc)
list <- list(A, B, C)

Here, CompA, CompB, and CompC are all identical, but the 'values' variables are all different. I want to rename all the variables in the list which contain "comp" to have the same name so I can merge all dataframes in the list into one dataframe by "Comp".

JointData <- join_all(list, by="Comp", type='left')

Does anyone know how to do this?

This question How to find common variables in a list of datasets & reshape them in R? seems to be the closest, but he's not actually renaming his variables as far as I can see.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Laura
  • 499
  • 5
  • 13

1 Answers1

2

We could use lapply and grep as follows:

lapply(list, function(x){setnames(x, old = grep("comp", names(x)), new = "Comp")})

Using your piece of code will give you, for the first 10 rows:

> JointData <- join_all(list, by="Comp", type='left')
> JointData
   Comp      valuesa      valuesb     valuesc
1     1  1.459809314  1.074395596  0.05459735
2     2 -1.442052931  0.259653583 -0.18003632
3     3  0.856110943  0.020583632 -2.23210988
4     4 -0.458347842 -0.472916330  0.03734474
5     5  0.722777326  0.372389619 -1.18025645
6     6 -1.079007191  1.812031538  0.81196831
7     7 -0.478990080 -0.298745059 -0.22902673
8     8 -0.015821375  0.414506210 -0.20809839
9     9 -0.844872308 -0.208205289  0.53310519
10   10  0.330617594 -0.177282871 -0.67203133
Gin_Salmon
  • 837
  • 1
  • 7
  • 19