English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R-Datenneuformung

Datenblöcke zusammenfügen

R-Sprache Datenblöcke zusammenfügen merge() Funktion.

merge() Funktionssyntax im folgenden Format:

# S3 Methode
merge(x, y, …)
# data.frame der S3 Methode 
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, ...)

Häufig verwendete Parameterbeschreibungen:

  • x, y:Datenblöcke

  • by, by.x, by.y:Bestimmt die Namen der übereinstimmenden Spalten in zwei Datenblöcken, Standardmäßig werden die gleichen Spaltennamen in beiden Datenblöcken verwendet.

  • all:Logischer Wert; all = L ist die Abkürzung für all.x = L und all.y = L, L kann TRUE oder FALSE sein.

  • all.x:Logischer Wert, Standardwert ist FALSE. Wenn TRUE, werden die Zeilen in x angezeigt, selbst wenn in y keine entsprechenden Übereinstimmungen vorhanden sind, werden die nicht übereinstimmenden Zeilen in y mit NA dargestellt.

  • all.y:Logischer Wert, Standardwert ist FALSE. Wenn TRUE, werden die Zeilen in y angezeigt, die eine Übereinstimmung aufweisen, selbst wenn in x keine entsprechenden Übereinstimmungen vorhanden sind, werden die nicht übereinstimmenden Zeilen in x mit NA dargestellt.

  • sort:Logischer Wert, ob die Spalte sortiert wird.

merge() Funktion und die JOIN-Funktion von SQL sind sehr ähnlich:

  • Natural join oder INNER JOIN:如果表中有至少一个匹配,则返回行

  • Left outer join oder LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行

  • Right outer join oder RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行

  • Full outer join oder FULL JOIN:只要其中一个表中存在匹配,则返回行

# data frame 1
df1 = data.frame(SiteId = c(1:6), Site = c("Google","w3codebox","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")
print("----- INNER JOIN -----)
print(df1)
# FULL JOIN
df2 = merge(x=df1,y=df2,by="SiteId",all=TRUE)
print("----- FULL JOIN -----)
print(df2)
# LEFT JOIN
df3 = merge(x=df1,y=df2,by="SiteId",all.x=TRUE)
print("----- LEFT JOIN -----)
print(df3)
# RIGHT JOIN
df4 = merge(x=df1,y=df2,by="SiteId",all.y=TRUE)
print("----- RIGHT JOIN -----)
print(df4)

Die Ausgabe des folgenden Codes ausführen:

[1] "----- INNER JOIN -----"
  SiteId  <NA>  Site Country
1      2   w3codebox  CN
2      4 Facebook  USA
3      6    Weibo  CN
[1] "----- FULL JOIN -----"
  SiteId  <NA>  Site Country.x Country.y
1      2   w3codebox  CN  CN
2      4 Facebook  USA  <NA>  USA
3      6    Weibo  CN  <NA>  CN
4      7     <NA>  <NA>  <NA> USA
5      8     <NA>  <NA>  <NA>
[1] "----- LEFT JOIN -----"
  SiteId  Site.x Country  Site.y Country.x Country.y
1      2   w3codebox  CN w3codebox  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   w3codebox  CN w3codebox  CN  CN
2      4 Facebook  USA Facebook  USA  USA
3      6    Weibo  CN  Weibo  CN  CN
4      7     <NA>  <NA>  <NA>  <NA>  <NA> USA
5      8     <NA>  <NA>  <NA>  <NA>  <NA>  <NA>

Datenintegration und -teilung

R-Sprache verwendet melt() und cast() Funktionen, um Daten zu integrieren und zu teilen.

  • melt() : Daten in langes Format in breites Format umwandeln.

  • cast() : Daten in breitem Format in langes Format umwandeln.

Das folgende Diagramm zeigt hervorragend die Funktionen der melt() und cast() -Funktionen (detaillierte Beispiele folgen):

melt() stacks each column of the dataset into one column, function syntax format: }}

melt(data, ..., na.rm = FALSE, value.name = "value")

Parameter description:

  • data: Dataset.

  • ...: Pass other parameters to other methods or parameters from other methods.

  • na.rm: Whether to delete NA values in the dataset.

  • value.name: Variable name, used to store values.

Before performing the following operations, we first install the dependent packages:

# Install libraries, MASS contains many statistical functions, tools, and datasets
install.packages("MASS", repos = "https://mirrors.ustc.edu.cn/CRAN/) 
  
# melt() and cast() functions require libraries 
install.packages("reshape2", repos = "https://mirrors.ustc.edu.cn/CRAN/) 
install.packages("reshape", repos = "https://mirrors.ustc.edu.cn/CRAN/)

Test example:

# Lade Bibliotheken
library(MASS) 
library(reshape2) 
library(reshape) 
  
# Erstelle Datenrahmen
id<- c(1, 1, 2, 2) 
zeit <- c(1, 2, 1, 2) 
x1 <- c(5, 3, 6, 2) 
x2 <- c(6, 5, 1, 4) 
mydata <- data.frame(id, zeit, x1, x2) 
  
# Original data frame
cat("Original data frame:\n") 
print(mydata) 
# Integration
md <- melt(mydata, id = c("id","zeit")) 
  
cat("\nAfter integration:\n") 
print(md)

Die Ausgabe des folgenden Codes ausführen:

Original data frame:
id zeit x1 x2
1  1    1  5  6
2  1    2  3  5
3  2    1  6  1
4  2    2  2  4
After integration:
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 merged data frames, dcast() returns a data frame, acast() returns a vector/Matrix/Array.

cast() function syntax format:

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: Merged data frame.

  • formula: Format of reshaped data, similar to x ~ y format, x as row label, y as column label.

  • fun.aggregate: Aggregate function, used to process value values.

  • margins: Vector of variable names (can include "grand_col" and "grand_row"), used to calculate margins, set TRUE to calculate all margins.

  • subset: Filter results by conditions, format similar to subset = .(variable=="length")

  • drop:Ob die Standardwerte beibehalten werden sollen.

  • value.var:Folgenden Feldern zu behandeln.

# Lade Bibliotheken
library(MASS) 
library(reshape2) 
library(reshape) 
  
# Erstelle Datenrahmen
id<- c(1, 1, 2, 2) 
zeit <- c(1, 2, 1, 2) 
x1 <- c(5, 3, 6, 2) 
x2 <- c(6, 5, 1, 4) 
mydata <- data.frame(id, zeit, x1, x2) 
# Integration
md <- melt(mydata, id = c("id","zeit")) 
# Drucke die neuformatierte Datenmenge mit der Funktion cast() aus 
cast.data <- cast(md, id~variable, mean) 
  
print(cast.data) 
  
cat("\n") 
zeit.cast <- cast(md, zeit~variable, mean) 
print(zeit.cast) 
cat("\n") 
id.zeit <- cast(md, id~zeit, mean) 
print(id.zeit) 
cat("\n") 
id.zeit.cast <- cast(md, id+zeit~variable) 
print(id.zeit.cast) 
cat("\n") 
id.variable.zeit <- cast(md, id+variable~zeit) 
print(id.variable.zeit) 
cat("\n") 
id.variable.zeit2 <- cast(md, id~variable+zeit) 
print(id.variable.zeit2)

Die Ausgabe des folgenden Codes ausführen:

id x1  x2
1  1  4 5.5
2  2  4 2.5
  zeit  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 zeit 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