
Combine several data frames by using id variables to match rows
Source:R/RbindAllCbindIdMatch.r
CbindIdMatch.Rd
Combine several data frames by using id variables to match rows
Arguments
- ...
Several data frames as several input parameters or a list of data frames
- addName
NULL or vector of strings used to name columns according to origin frame
- sep
A character string to separate when addName apply
- idNames
Names of a id variable within each data frame
- idNames1
Names of variables in first data frame that correspond to the id variable within each data frame
- addLast
When TRUE addName will be at end
Details
The first data frame is the basis and the other frames will be matched by using id-variables. The default id-variables are the first variable in each frame. Corresponding variables with the same name in first frame is assumed. An id-variable is not needed if the number of rows is one or the same as the first frame. Then the element of idNames can be set to a string with zero length.
See also
RbindAll
(same example data)
Examples
zA <- data.frame(idA = 1:10, idB = rep(10 * (1:5), 2), idC = rep(c(100, 200), 5),
idC2 = c(100, rep(200, 9)), idC3 = rep(100, 10),
idD = 99, x = round(rnorm(10), 3), xA = round(runif(10), 2))
zB <- data.frame(idB = 10 * (1:5), x = round(rnorm(5), 3), xB = round(runif(5), 2))
zC <- data.frame(idC = c(100, 200), x = round(rnorm(2), 3), xC = round(runif(2), 2))
zD <- data.frame(idD = 99, x = round(rnorm(1), 3), xD = round(runif(1), 2))
CbindIdMatch(zA, zB, zC, zD)
#> Warning: Column names not unique
#> idA idB idC idC2 idC3 idD x xA x xB x xC x xD
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71 -0.374 0.27
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.770 0.07 -0.374 0.27
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.079 0.71 -0.374 0.27
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.770 0.07 -0.374 0.27
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.079 0.71 -0.374 0.27
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.770 0.07 -0.374 0.27
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.079 0.71 -0.374 0.27
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.770 0.07 -0.374 0.27
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.079 0.71 -0.374 0.27
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.770 0.07 -0.374 0.27
CbindIdMatch(a = zA, b = zB, c = zC, d = zD, idNames = c("", "idB", "idC", ""))
#> a_idA a_idB a_idC a_idC2 a_idC3 a_idD a_x a_xA b_x b_xB c_x c_xC
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.770 0.07
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.079 0.71
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.770 0.07
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.079 0.71
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.770 0.07
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.079 0.71
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.770 0.07
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.079 0.71
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.770 0.07
#> d_idD d_x d_xD
#> 1 99 -0.374 0.27
#> 2 99 -0.374 0.27
#> 3 99 -0.374 0.27
#> 4 99 -0.374 0.27
#> 5 99 -0.374 0.27
#> 6 99 -0.374 0.27
#> 7 99 -0.374 0.27
#> 8 99 -0.374 0.27
#> 9 99 -0.374 0.27
#> 10 99 -0.374 0.27
CbindIdMatch(a = zA, b = zB, c = zC, d = zD, idNames1 = c("", "idB", "idC2", ""))
#> a_idA a_idB a_idC a_idC2 a_idC3 a_idD a_x a_xA b_x b_xB c_x c_xC
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.770 0.07
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.770 0.07
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.770 0.07
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.770 0.07
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.770 0.07
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.770 0.07
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.770 0.07
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.770 0.07
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.770 0.07
#> d_idD d_x d_xD
#> 1 99 -0.374 0.27
#> 2 99 -0.374 0.27
#> 3 99 -0.374 0.27
#> 4 99 -0.374 0.27
#> 5 99 -0.374 0.27
#> 6 99 -0.374 0.27
#> 7 99 -0.374 0.27
#> 8 99 -0.374 0.27
#> 9 99 -0.374 0.27
#> 10 99 -0.374 0.27
CbindIdMatch(a = zA, b = zB, c = zC, d = zD, idNames1 = c("", "idB", "idC3", ""))
#> Warning: All rows not used
#> a_idA a_idB a_idC a_idC2 a_idC3 a_idD a_x a_xA b_x b_xB c_x c_xC
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.079 0.71
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.079 0.71
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.079 0.71
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.079 0.71
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.079 0.71
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.079 0.71
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.079 0.71
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.079 0.71
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.079 0.71
#> d_idD d_x d_xD
#> 1 99 -0.374 0.27
#> 2 99 -0.374 0.27
#> 3 99 -0.374 0.27
#> 4 99 -0.374 0.27
#> 5 99 -0.374 0.27
#> 6 99 -0.374 0.27
#> 7 99 -0.374 0.27
#> 8 99 -0.374 0.27
#> 9 99 -0.374 0.27
#> 10 99 -0.374 0.27
CbindIdMatch(zA, zB, zC, zD, addName = c("", "bbb", "ccc", "ddd"), sep = ".", addLast = TRUE)
#> idA idB idC idC2 idC3 idD x xA x.bbb xB.bbb x.ccc xC.ccc x.ddd
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71 -0.374
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.770 0.07 -0.374
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.079 0.71 -0.374
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.770 0.07 -0.374
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.079 0.71 -0.374
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.770 0.07 -0.374
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.079 0.71 -0.374
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.770 0.07 -0.374
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.079 0.71 -0.374
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.770 0.07 -0.374
#> xD.ddd
#> 1 0.27
#> 2 0.27
#> 3 0.27
#> 4 0.27
#> 5 0.27
#> 6 0.27
#> 7 0.27
#> 8 0.27
#> 9 0.27
#> 10 0.27
try(CbindIdMatch(X = zA, Y = zA[, 4:5], Z = zC, idNames = NULL)) # Error
#> Error in CbindIdMatch(X = zA, Y = zA[, 4:5], Z = zC, idNames = NULL) :
#> Element of idNames/idNames1 missing when needed
CbindIdMatch(X = zA, Y = zA[, 4:5], Z = zD, idNames = NULL) # Ok since equal NROW or NROW==1
#> X_idA X_idB X_idC X_idC2 X_idC3 X_idD X_x X_xA Y_idC2 Y_idC3 Z_idD Z_x
#> 1 1 10 100 100 100 99 0.622 0.70 100 100 99 -0.374
#> 2 2 20 200 200 100 99 1.148 0.69 200 100 99 -0.374
#> 3 3 30 100 200 100 99 -1.822 0.03 200 100 99 -0.374
#> 4 4 40 200 200 100 99 -0.247 0.23 200 100 99 -0.374
#> 5 5 50 100 200 100 99 -0.244 0.30 200 100 99 -0.374
#> 6 6 10 200 200 100 99 -0.283 0.64 200 100 99 -0.374
#> 7 7 20 100 200 100 99 -0.554 0.48 200 100 99 -0.374
#> 8 8 30 200 200 100 99 0.629 0.43 200 100 99 -0.374
#> 9 9 40 100 200 100 99 2.065 0.71 200 100 99 -0.374
#> 10 10 50 200 200 100 99 -1.631 0.95 200 100 99 -0.374
#> Z_xD
#> 1 0.27
#> 2 0.27
#> 3 0.27
#> 4 0.27
#> 5 0.27
#> 6 0.27
#> 7 0.27
#> 8 0.27
#> 9 0.27
#> 10 0.27
CbindIdMatch(list(a = zA, b = zB, c = zC, d = zD)) # List is alternative input
#> a_idA a_idB a_idC a_idC2 a_idC3 a_idD a_x a_xA b_x b_xB c_x c_xC
#> 1 1 10 100 100 100 99 0.622 0.70 -0.914 0.97 0.079 0.71
#> 2 2 20 200 200 100 99 1.148 0.69 0.468 0.39 0.770 0.07
#> 3 3 30 100 200 100 99 -1.822 0.03 0.363 0.46 0.079 0.71
#> 4 4 40 200 200 100 99 -0.247 0.23 -1.305 0.32 0.770 0.07
#> 5 5 50 100 200 100 99 -0.244 0.30 0.738 0.17 0.079 0.71
#> 6 6 10 200 200 100 99 -0.283 0.64 -0.914 0.97 0.770 0.07
#> 7 7 20 100 200 100 99 -0.554 0.48 0.468 0.39 0.079 0.71
#> 8 8 30 200 200 100 99 0.629 0.43 0.363 0.46 0.770 0.07
#> 9 9 40 100 200 100 99 2.065 0.71 -1.305 0.32 0.079 0.71
#> 10 10 50 200 200 100 99 -1.631 0.95 0.738 0.17 0.770 0.07
#> d_x d_xD
#> 1 -0.374 0.27
#> 2 -0.374 0.27
#> 3 -0.374 0.27
#> 4 -0.374 0.27
#> 5 -0.374 0.27
#> 6 -0.374 0.27
#> 7 -0.374 0.27
#> 8 -0.374 0.27
#> 9 -0.374 0.27
#> 10 -0.374 0.27