Here, we show how to merge or combine dataframes, columns or vectors, matrices and lists in R with merge(), rbind(), and cbind().

All the functions are from the "base" package.

Summary Functions for Merging or Binding Data Objects in R
Function Usage
merge() Merge two dataframes
rbind() Join data objects vertically or by binding rows
cbind() Join data objects horizontally or by binding columns

1 Merge and Combine Dataframes in R

Using the two dataframes below:

Round1 = data.frame(Group = c("A", "B", "B"),
                    Captain = c("Dave", "Sarah", "Stan"),
                    Points = c(8, 9, 7),
                    Position = c(2, 1, 3))
Round1
  Group Captain Points Position
1     A    Dave      8        2
2     B   Sarah      9        1
3     B    Stan      7        3
Round2 = data.frame(Group = c("A", "B", "B"),
                    Captain = c("Dave", "Sarah", "Lisa"),
                    Points = c(7, 10, 8),
                    Position = c(3, 1, 2))
Round2
  Group Captain Points Position
1     A    Dave      7        3
2     B   Sarah     10        1
3     B    Lisa      8        2

Merge two dataframes by one column:

merge(Round1,  Round2, by = c("Group"))
  Group Captain.x Points.x Position.x Captain.y Points.y Position.y
1     A      Dave        8          2      Dave        7          3
2     B     Sarah        9          1     Sarah       10          1
3     B     Sarah        9          1      Lisa        8          2
4     B      Stan        7          3     Sarah       10          1
5     B      Stan        7          3      Lisa        8          2

Merge two dataframes by two or more columns:

You can merge columns by setting the "by" argument.

merge(Round1,  Round2, by = c("Group", "Captain"))
  Group Captain Points.x Position.x Points.y Position.y
1     A    Dave        8          2        7          3
2     B   Sarah        9          1       10          1

To include rows where the selected columns do not match, set the "all.x" and "all.y" arguments to TRUE.

merge(Round1,  Round2, by = c("Group", "Captain"), all.x = TRUE, all.y = TRUE)
  Group Captain Points.x Position.x Points.y Position.y
1     A    Dave        8          2        7          3
2     B    Lisa       NA         NA        8          2
3     B   Sarah        9          1       10          1
4     B    Stan        7          3       NA         NA

Combine two dataframes vertically or by binding rows:

rbind(Round1, Round2)
  Group Captain Points Position
1     A    Dave      8        2
2     B   Sarah      9        1
3     B    Stan      7        3
4     A    Dave      7        3
5     B   Sarah     10        1
6     B    Lisa      8        2

Combine two dataframes horizontally or by binding columns:

cbind(Round1, Round2)
  Group Captain Points Position Group Captain Points Position
1     A    Dave      8        2     A    Dave      7        3
2     B   Sarah      9        1     B   Sarah     10        1
3     B    Stan      7        3     B    Lisa      8        2

2 Combine Columns or Vectors in R

Using the sample columns or vectors below:

vec1 = c(2, 4, 6)
vec2 = c(10, 50, 100)

Combine two columns or vectors into a new one:

vec_join = c(vec1, vec2)
vec_join
[1]   2   4   6  10  50 100

Combine two columns or vectors vertically or by binding rows:

rbind(vec1, vec2)
     [,1] [,2] [,3]
vec1    2    4    6
vec2   10   50  100

Combine two columns or vectors horizontally or by binding columns:

cbind(vec1, vec2)
     vec1 vec2
[1,]    2   10
[2,]    4   50
[3,]    6  100

3 Combine Matrices in R

Using the sample matrices below:

mat1 = diag(c(1, 2), nrow = 2, ncol = 2)
mat1
     [,1] [,2]
[1,]    1    0
[2,]    0    2
mat2 = matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
mat2
     [,1] [,2]
[1,]    1    2
[2,]    3    4

Combine two matrices vertically or by binding rows:

rbind(mat1, mat2)
     [,1] [,2]
[1,]    1    0
[2,]    0    2
[3,]    1    2
[4,]    3    4

Combine two matrices horizontally or by binding columns:

cbind(mat1, mat2)
     [,1] [,2] [,3] [,4]
[1,]    1    0    1    2
[2,]    0    2    3    4

4 Combine Lists in R

Using the sample lists below:

vec1 = c("a", "b", "c")
mat1 = matrix(c(c("a", "b"), c("x", "y")), ncol = 2, byrow = FALSE)
ls1 = list("vector" = vec1, "matrix" = mat1)
ls1
$vector
[1] "a" "b" "c"

$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 
vec2 = 1:4
dtf2 = data.frame(Color = c("Cyan", "Yellow"), 
                   Size = c(4, 6))
ls2 = list(vec2, dtf2)
ls2
[[1]]
[1] 1 2 3 4

[[2]]
   Color Size
1   Cyan    4
2 Yellow    6

Combine two lists:

Here, we combine the "ls1" and "ls2" lists.

ls3 = list(ls1, ls2)
ls3
[[1]]
[[1]]$vector
[1] "a" "b" "c"

[[1]]$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 


[[2]]
[[2]][[1]]
[1] 1 2 3 4

[[2]][[2]]
   Color Size
1   Cyan    4
2 Yellow    6

Combine lists by having one list contain another list:

Here, we combine the "ls1" list and objects in the "ls2" list into one list.

ls4 = list(ls1, vec2, dtf2)
ls4
[[1]]
[[1]]$vector
[1] "a" "b" "c"

[[1]]$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 


[[2]]
[1] 1 2 3 4

[[3]]
   Color Size
1   Cyan    4
2 Yellow    6

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes