Kategorien
Data Science

R: Merge function

The merge function in R allows the user to combine multiple data frames into a single data frame. Imagine you have information about students (name and average grades) in one data frame (A) and the student’s age in the data frame (B).
However, the goal is to access age and average grades easily. Therefore, you need to merge the data.

There are 3 options to merge your data:
Option 1: inner join
Return only the rows in which the left table have matching keys in the right table.
Option 2: outer join
Returns all rows from both tables, join records from the left which have matching keys in the right table.
Option 3: left join
Return all rows from the left table, and any rows with matching keys from the right table.
Option 4: right join
Return all rows from the right table, and any rows with matching keys from the left table.

Setup:

# Data frame A
name <- c("Tom","Jack","Johanna","Simon","Dario")
grade <- c(5,5.5,4,6,3.5)
A <- data.frame(cbind(A,as.numeric(A2)))
colnames(A) <- c("name","grade")

# Data frame B
B <- c("Tom","Johanna","Lukas")
age <- c(21,22,23)
B <- data.frame(cbind(B,as.numeric(age)))
colnames(B) <- c("name","age")
Inner join:
 merge(x = A, y = B, by = "name",) 
Outer join
 merge(x = A, y = B, by = "name", all = TRUE) 
Left outer
 merge(x = A, y = B, by = "name", all.x = TRUE) 
Right outer 
merge(x = A, y = B, by = "name", all.y = TRUE) 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.