R Data Reshaping
Merging data frames
R merges data frames using the merge() function in Vue 3.
merge() function, with the following syntax:
# S3 方法
merge(x, y, …)
# S3 methods for data.frame
merge(x, y, by = intersect(names(x), names(y)),
by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,
sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE,
incomparables = NULL, …)
Description of commonly used parameters:
- x, y: data frames
- by, by.x, by.y: Specify the matching column names in the two data frames; by default, columns with the same name in both data frames are used.
- all: logical value; all = L is shorthand for all.x = L and all.y = L, where L can be TRUE or FALSE.
- all.x: logical value, default FALSE. If TRUE, display all rows in x even if there is no matching row in y; unmatched rows in y are represented by NA.
- all.y: logical value, default FALSE. If TRUE, display all rows in y even if there is no matching row in x; unmatched rows in x are represented by NA.
- sort: logical value, whether to sort the columns.
The merge() function is very similar to SQL's JOIN:

- Natural join or INNER JOIN: Return rows if there is at least one match in the tables
- Left outer join or LEFT JOIN: Return all rows from the left table even if there is no match in the right table
- Right outer join or RIGHT JOIN: Return all rows from the right table even if there is no match in the left table
- Full outer join or FULL JOIN: Return rows whenever there is a match in either table
Example
data frame 1
df1
=
data.frame
(
SiteId
=
c
(
1
:
6
)
, Site
=
c
(
"Google"
,
"Runoob"
,
"Taobao"
,
"Facebook"
,
"Zhihu"
,
"Weibo"
)
)
data frame 2
df2
=
data.frame
(
SiteId
=
c
(
2
,
4
,
6
,
7
,
8
)
, Country
=
c
(
"CN"
,
"USA"
,
"CN"
,
"USA"
,
"IN"
)
)
INNER JOIN
df1
=
merge
(
x
=
df1,y
=
df2,
by
=
"SiteId"
)
(
"----- INNER JOIN -----"
)
(
df1
)
FULL JOIN
df2
=
merge
(
x
=
df1,y
=
df2,
by
=
"SiteId"
,
all
=
TRUE
)
(
"----- FULL JOIN -----"
)
(
df2
)
LEFT JOIN
df3
=
merge
(
x
=
df1,y
=
df2,
by
=
"SiteId"
,all.
x
=
TRUE
)
(
"----- LEFT JOIN -----"
)
(
df3
)
RIGHT JOIN
df4
=
merge
(
x
=
df1,y
=
df2,
by
=
"SiteId"
,all.
y
=
TRUE
)
(
"----- RIGHT JOIN -----"
)
(
df4
)
The output of the code above is:
[1] "----- INNER JOIN -----"
SiteId Site Country
1 2 Runoob CN
2 4 Facebook USA
3 6 Weibo CN
[1] "----- FULL JOIN -----"
SiteId Site Country.x Country.y
1 2 Runoob CN CN
2 4 Facebook USA USA
3 6 Weibo CN CN
4 7 USA
5 8 IN
[1] "----- LEFT JOIN -----"
SiteId Site.x Country Site.y Country.x Country.y
1 2 Runoob CN Runoob CN CN
2 4 Facebook USA Facebook USA USA
3 6 Weibo CN Weibo CN CN
[1] "----- RIGHT JOIN -----"
SiteId Site.x Country Site.y Country.x Country.y
1 2 Runoob CN Runoob CN CN
2 4 Facebook USA Facebook USA USA
3 6 Weibo CN Weibo CN CN
4 7 USA
5 8 IN
Data aggregation and splitting
R uses the melt() and cast() function to aggregate and split data.
- melt(): converts wide-format data to long format.
- cast(): converts long-format data to wide format.
The following figure clearly shows the functions of melt() and cast() (detailed in the examples below):

melt() stacks each column of a dataset into a single column; the function syntax is:
melt(data, ..., na.rm = FALSE, value.name = "value")
Parameter Description:
- data: the dataset.
- ...: additional arguments passed to or from other methods.
- na.rm: whether to remove NA values from the dataset.
- value.name: the variable name used to store the values.
Before performing the following operations, we first install the dependency package:
# 安装库,MASS 包含很多统计相关的函数,工具和数据集
install.packages("MASS", repos = "https://mirrors.ustc.edu.cn/CRAN/")
# melt() and cast() functions require the library
install.packages("reshape2", repos = "https://mirrors.ustc.edu.cn/CRAN/")
install.packages("reshape", repos = "https://mirrors.ustc.edu.cn/CRAN/")
Test example:
Example
Load the library
library
(
MASS
)
library
(
reshape2
)
library
(
reshape
)
Create a data frame
id
<
(
1
,
1
,
2
,
2
)
time
<
(
1
,
2
,
1
,
2
)
x1
<
(
5
,
3
,
6
,
2
)
x2
<
(
6
,
5
,
1
,
4
)
mydata
<
(
id
,
time, x1, x2
)
Original data frame
cat
(
"Original data frame:\n"
)
(
mydata
)
Integrate
md
<
(
mydata,
id= c
(
"id"
,
"time"
)
)
cat
(
"\nAfter aggregation:\n"
)
(
md
)
The output of the code above is:
原始数据框:
id time x1 x2
1 1 1 5 6
2 1 2 3 5
3 2 1 6 1
4 2 2 2 4
Integrated:
id time variable value
1 1 1 x1 5
2 1 2 x1 3
3 2 1 x1 6
4 2 2 x1 2
5 1 1 x2 6
6 1 2 x2 5
7 2 1 x2 1
8 2 2 x2 4
The cast function is used to restore a merged data frame; dcast() returns a data frame, and acast() returns a vector/matrix/array.
The cast() function syntax is:
dcast(
data,
formula,
fun.aggregate = NULL,
...,
margins = NULL,
subset = NULL,
fill = NULL,
drop = TRUE,
value.var = guess_value(data)
)
acast(
data,
formula,
fun.aggregate = NULL,
...,
margins = NULL,
subset = NULL,
fill = NULL,
drop = TRUE,
value.var = guess_value(data)
)
Parameter Description:
- data: the merged data frame.
- formula: the format of the reshaped data, similar to x ~ y, where x is the row label and y is the column label.
- fun.aggregate: the aggregation function used to process the value values.
- margins: a vector of variable names (which can include "grand_col" and "grand_row") used to compute margins; set to TRUE to compute all margins.
- subset: conditionally filters the results, in a format similar to subset = .(variable=="length")。
- drop: whether to keep the default values.
- value.var: followed by the field to be processed.
Example
Load the library
library
(
MASS
)
library
(
reshape2
)
library
(
reshape
)
Create a data frame
id
<-
c
(
1
,
1
,
2
,
2
)
time
<-
c
(
1
,
2
,
1
,
2
)
x1
<-
c
(
5
,
3
,
6
,
2
)
x2
<-
c
(
6
,
5
,
1
,
4
)
mydata
<-
data.frame
(
id,
time
, x1, x2
)
Integrate
md
<-
melt
(
mydata, id
=
c
(
"id"
,
"time"
)
)
Print recasted dataset using cast() function
cast.
data
<-
cast
(
md, id~variable,
mean
)
(
cast.
data
)
cat
(
"\n"
)
time.
cast
<-
cast
(
md,
time
~variable,
mean
)
(
time.
cast
)
cat
(
"\n"
)
id.
time
<-
cast
(
md, id~
time
,
mean
)
(
id.
time
)
cat
(
"\n"
)
id.
time
.
cast
<-
cast
(
md, id
time
~variable
)
(
id.
time
.
cast
)
cat
(
"\n"
)
id.
variable
.
time
<-
cast
(
md, id
variable~
time
)
(
id.
variable
.
time
)
cat
(
"\n"
)
id.
variable
.
time2
<-
cast
(
md, id~variable
time
)
(
id.
variable
.
time2
)
The output of the code above is:
id x1 x2
1 1 4 5.5
2 2 4 2.5
time x1 x2
1 1 5.5 3.5
2 2 2.5 4.5
id 1 2
1 1 5.5 4
2 2 3.5 3
id time x1 x2
1 1 1 5 6
2 1 2 3 5
3 2 1 6 1
4 2 2 2 4
id variable 1 2
1 1 x1 5 3
2 1 x2 6 5
3 2 x1 6 2
4 2 x2 1 4
id x1_1 x1_2 x2_1 x2_2
1 1 5 3 6 5
2 2 6 2 1 4