How to merge data frame in R?
Merge and modify 2 data frames with R
Merge data frames is a common task that we can execute. We will discuss about the ways to achieve this.
Add columns with merge()
The merge() function is a. common function for this kind of task. Here is how it works:
In this example, each attribute x and y are a data frame. This instruction will merge the two datasets by selecting the identical value located on a column with the same name. Moreover, it will delete the values not included in one and the other dataset. This function is extended by several attributes allow to merge the datasets with different parameters.
by = #Indicate the value of the column to merge in the two data frame by.x = # Indicate the value of the column to merge in the data frame x by.y = # Indicate the value of the column to merge in the data frame y all = # Keep all the lines of the data frames all.x = # Keep all the lines in the data frame x all.y = # Keep all the lines in the data frame y
Add lines with rbind()
The rbind() function is easier. It can be used to merge two data frames by adding the values of each. It's the ideal function to complete the values of a data frame with an iterative wave.
Here is how it works:
This example will add the lines of the data frame y to the data frame x.