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.
Function | Usage |
merge() |
Merge two dataframes |
rbind() |
Join data objects vertically or by binding rows |
cbind() |
Join data objects horizontally or by binding columns |
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:
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.
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
.
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:
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:
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
Using the sample columns or vectors below:
Combine two columns or vectors into a new one:
[1] 2 4 6 10 50 100
Combine two columns or vectors vertically or by binding rows:
[,1] [,2] [,3]
vec1 2 4 6
vec2 10 50 100
Combine two columns or vectors horizontally or by binding columns:
vec1 vec2
[1,] 2 10
[2,] 4 50
[3,] 6 100
Using the sample matrices below:
[,1] [,2]
[1,] 1 0
[2,] 0 2
[,1] [,2]
[1,] 1 2
[2,] 3 4
Combine two matrices vertically or by binding rows:
[,1] [,2]
[1,] 1 0
[2,] 0 2
[3,] 1 2
[4,] 3 4
Combine two matrices horizontally or by binding columns:
[,1] [,2] [,3] [,4]
[1,] 1 0 1 2
[2,] 0 2 3 4
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.
[[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.
[[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
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes