Skip to main content

Joining observation units with dplyr

Today I would like to show examples of different ways you can join data frames. Let's define and display them first. In first data.frame I will collect some information's about certain. It will contain name, high and nationality.

df1<-data.frame(name=c("Ania","Marek","Kamil","Joanna","Patrice"),high=c(178,190,175,168,175),nationality=c("polish","polish","polish","polish","french"))
df1
      name high nationality
1    Ania   178      polish
2   Marek   190      polish
3   Kamil   175      polish
4  Joanna   168      polish
5 Patrice   175      french

In second data.frame I will put observation about other group, containing their name and weight. What does this two data.frame have in common ? We can see that both contain column with the name of the person and what is more some person like Ania and Patrice are being describe in both data.frame. 

df2<-data.frame(name=c("Ania","Julia","Patrice","Jim"),weight=c(67,55,75,75)) 
df2 
       name weight
1    Ania     67
2   Julia     55
3 Patrice     75
4     Jim     75

Let's see now different possibilities of joining these elements:
library(dplyr)
inner_join(df1,df2) 

Inner_join() will return observations which are in both df1 and df2. All columns both from df1 and df2 will be present.

Joining, by = "name"
     name high nationality weight
1    Ania   178      polish     67
2 Patrice   175      french     75

left_join(df1,df2) 
Left_join() will return columns of df1 and df2 containing unique observation of df1 and those which exists both in df1 and df2.

Joining, by = "name"
     name high nationality weight
1    Ania   178      polish     67
2   Marek   190      polish     NA
3   Kamil   175      polish     NA
4  Joanna   168      polish     NA
5 Patrice   175      french     75

right_join(df1,df2) 

Right_join() will return columns of df1 and df2 containing unique observations of df2 and those which exists both in df1 and df2.

joining, by = "name"
     name high nationality weight
1    Ania   178      polish     67
2 Patrice   175      french     75
3   Julia    NA        <NA>     55
4     Jim    NA        <NA>     75

full_join(df1,df2) 

Full-join() will return columns of df1 and df2 containing all observations present in df1 and df2.

Joining, by = "name"
     name high nationality weight
1    Ania   178      polish     67
2   Marek   190      polish     NA
3   Kamil   175      polish     NA
4  Joanna   168      polish     NA
5 Patrice   175      french     75
6   Julia    NA        <NA>     55
7     Jim    NA        <NA>     75

anti_join(df1,df2) 
Anti_join() excludes rows from df1 which are present in df2.

Joining, by = "name"
    name high nationality
1  Marek   190      polish
2  Kamil   175      polish
3 Joanna   168      polish

semi_join(df1,df2)

Semi_join() will match the rows.

Joining, by = "name"
     name high nationality
1    Ania   178      polish
2 Patrice   175      french

Comments

Popular posts from this blog

Model Residuals in Time Series Data

Residuals are the indicator of the model quality. Based on Rob J Hyndman's book "Forecasting: Principles & Practice", residuals in forecasting is difference between observed value and its forecast based on all previous observations. Residuals are useful in checking whether a model has adequately captured the information in the data. All the patterns should be in the model, only randomness remains in the residuals. Therefore the ideal model has to be: uncorrelated has zero mean and useful properties are: constant variance  be normally distributed First I will activate some useful libraries we will be using. library(fpp) library(forecast) For our example I will use dowjones index as a data set. The idea will be to set up already well know simple models like: Mean Model, Naive model and Drift Model. In previous post I described  it more detailed. Next, knowing what attributes  the ideal model should  have we can check which one of those 3 are quite good or  def...

The Power of dplyr in R - part 1

The dplyr is one of the library in Tidyverse package. In other word a collection of R libraries that work together in order to achieve clean and tidy data. I have started the discovery of its content while learning process of data pre-processing, data aggregation. It turns out to be very efficient, easy to use and fast tool so lot of people including me use it very often. It will help you with manipulation of data.frame, queries, sorting, summary statistics,  joining tables and more.  My math’s teacher used to say that when you are trying to solve the problem it matters which way you choose to achieve the goal. It is up to us to choose the most efficient tool so all the process will go smoothly. This is the reason why dplyr package  is worth learning! It allows you not only to do your tasks but it will do it in quite easy and fast way. Pay attention for data you are taking while using dplyr - it can be tibble or  data.frame.  I will use mtcars dataset which is i...

The Power of dplyr in R - part 3

Today I would like to present pipe operator which simplify our code and makes it more readable. As we can see all of the dplyr functions take a data frame (or tibble) as the first argument. Dplyr provides the %>% operator from magrittr that chains the functions so x %>% f(y) turns into f(x, y). Therefore  the result from one step is then “piped” into the next step. We will use pipe operator in further examples.  Additionally we will focus on grouping, ordering and summarising functions. As previously I will continue using mtcars dataset which is included in your R base program. count() #count the unique values of one or more variables   n()  n_distinct() #number of unique observation found in a category  group_by() # group by a column, allows to group operation in the “split-apply-combine" concept   library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat...