Elongating a Dataset using methods from dplyr & tidyr

Setting the environment

These are the R libraries we will need for this demo:



Read .csv file into R

    The .csv file that was generated in the previous block of code was uploaded to the author’s github accout. We can read the .csv into an R data.frame via the URL link to the raw data:

##                NA.     Male   Male.1    Male.2     Male.3   Male.4   Female
## 1             <NA>    No HS       HS Associate Bachelor's Graduate    No HS
## 2         T066_008 T066_009 T066_010  T066_011   T066_012 T066_013 T066_014
## 3 Northeast Region    24801    36714     44765      65177    89449    16523
## 4   Midwest Region    23530    33606     41180      59243    77325    15471
## 5     South Region    22065    31552     40097      59711    81744    14659
## 6      West Region    22554    32289     42127      62069    86476    15250
##   Female.1  Female.2   Female.3 Female.4
## 1       HS Associate Bachelor's Graduate
## 2 T066_015  T066_016   T066_017 T066_018
## 3    24251     31455      45593    61203
## 4    21713     26944      39534    54246
## 5    21530     27243      41210    53100
## 6    23230     29402      42527    57915

Tidy the data.frame with tidyr & dplyr

    This is a very small data.frame, but it can be used to illustrate a big point: tidy data facilitate visualization & downstream analysis. Tidying, or wrangling the data is often necessary to perform before more glamorous techniques (e.g. modeling) can be applied to a dataset. We will only be performing some brief analysis and visualization of this data.frame, but the main point is to observe how easy these later steps occur if we take the time to properly format the data.frame.
    In this demo, the ‘eduDATA_df’ data.frame will be changed from the present ‘wide’ version into a longer, ‘melted’ version; it will hold the same information, but will be rearranged to facilitate visualization and analysis.


Let’s start cleaning!



## # A tibble: 6 x 3
##   Region           Category       Total
##   <chr>            <chr>          <dbl>
## 1 Northeast Region Male.NoHS      24801
## 2 Northeast Region Male.HS        36714
## 3 Northeast Region Male.Associate 44765
## 4 Northeast Region Male.Bachelors 65177
## 5 Northeast Region Male.Graduate  89449
## 6 Northeast Region Female.NoHS    16523


    The data.frame has been transformed from a wide format with multiple observations in a single row, to a much longer version. Now each value from the data.frame has it’s own row. However, in the process of simplifying the double header, two features of the data were combined into one column, ‘Category’.
    Our next step is to seperate the label information for Gender & Level of Education into seperate columns. We will do this using tidyr seperate() method:

## # A tibble: 6 x 4
##   Region           Gender eduLevel  Total
##   <chr>            <chr>  <chr>     <dbl>
## 1 Northeast Region Male   NoHS      24801
## 2 Northeast Region Male   HS        36714
## 3 Northeast Region Male   Associate 44765
## 4 Northeast Region Male   Bachelors 65177
## 5 Northeast Region Male   Graduate  89449
## 6 Northeast Region Female NoHS      16523


    Excellent! Now the data.frame is organized in a format that wil facilitate use with functions such as ggplot2 plotting methods. We can now begin to tackle the analysis questions that were mentioned in the class discussion associated with this dataset.

Question 1: Which region appears to have the highest proportions of HS graduates as the highest educational attainment?

Visualize the data in a way that best addresses this question:

    From the figure above, we can see that the proportions per education level are comparable between each region. To address the Question: The Midwest Region has the highest proportion of people with ‘High School’ as their highest educational achievement.

Question 2:Which education level has the highest discrepancy between Males and Females?

(NOTE: This question was not mentioned in the DATA607 discussion board)

Visualize the data in a way that best addresses this question:


    The differences between Male and Female are too slim to appreciate in this figure. Next, calculate the discrepancy:

## # A tibble: 5 x 4
##   eduLevel  Female   Male discrepancy_Percent
##   <chr>      <dbl>  <dbl>               <dbl>
## 1 Associate 0.174  0.172               0.130 
## 2 Bachelors 0.255  0.252               0.257 
## 3 Graduate  0.342  0.343               0.149 
## 4 HS        0.137  0.137               0.0554
## 5 NoHS      0.0934 0.0952              0.182

    Again, the discrepancies are very slim, but from the output of our calculations, we can see that the largest discrepancy between Male & Female proportions is found for the Bachelors education level. By looking at the bar plot above, we can see that Females with bachelors have a slightly higher proportion than Males with bachelors. However, larger discrepancies might be found between the genders if we are to look at the data by region.

In Closing

    This demo used a small .csv file to demonstrate the importance tidying datasets. The original format of the data is very easy on human eyes: it can be scanned for the gist with ease. However, the wide format does not facilitate visualization. dplyr and tidyr provide methods to reshape data into a format that helps downstream analysis.