diff --git a/_site/blog.html b/_site/blog.html index 2a34b64..f8ff882 100644 --- a/_site/blog.html +++ b/_site/blog.html @@ -156,7 +156,7 @@ ul.task-list li input[type="checkbox"] { +
Categories
All (6)
@@ -169,7 +169,63 @@ ul.task-list li input[type="checkbox"] {
-
+
+
+

+
+ + +
+
+
+

image of example data

+
+ + +
+
@@ -197,7 +253,7 @@ Kyle Belanger
-
+
@@ -225,7 +281,7 @@ Kyle Belanger
-
+
@@ -253,7 +309,7 @@ Kyle Belanger
-
+
diff --git a/_site/listings.json b/_site/listings.json index f912281..612f1e1 100644 --- a/_site/listings.json +++ b/_site/listings.json @@ -2,6 +2,8 @@ { "listing": "/blog.html", "items": [ + "/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html", + "/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html", "/posts/2020-02-13_basic-who-TB-data/basic-exploration-of-who-tuberculosis-data.html", "/posts/2020-02-10_line-graphs-and-interactivity/line-graphs-and-interactivity.html", "/posts/2020-01-29_facets-and-humility/facets-and-a-lesson-in-humility.html", diff --git a/_site/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png b/_site/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png new file mode 100644 index 0000000..58c7338 Binary files /dev/null and b/_site/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png differ diff --git a/_site/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html b/_site/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html new file mode 100644 index 0000000..2f35db1 --- /dev/null +++ b/_site/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html @@ -0,0 +1,625 @@ + + + + + + + + + + + +Kyle Belanger - Importing Excel Data with Multiple Header Rows + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+ +
+
+
+

Importing Excel Data with Multiple Header Rows

+

A solution for importing Excel Data that contains two header rows.

+
+
+ + +
+ +
+
Author
+ +
+ +
+
Published
+
+

June 22, 2020

+
+
+ + +
+ + +
+ + +
+ + + +
+ + + + +
+

Problem

+

Recently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and then the second row listed a category within that group. Searching goggle I couldn’t really find a good example of what I was looking for, so I am putting it here in hopes of helping someone else!

+
+
+

Example Data

+

I have created a small Excel file to demonstrate what I am talking about. Download it here. This is the data from Excel. image of example data

+
+
+

Check Data

+

First we will read the file in using the package readxl and view the data without doing anything special to it.

+
+
library(readxl)                  # load the readxl library
+library(tidyverse)               # load the tidyverse for manipulating the data
+file_path <- "example_data.xlsx" # set the file path
+ds0 <- read_excel(file_path)     # read the file
+ds0
+
+
# A tibble: 7 × 7
+  Name   `Test 1` ...3  ...4  `Test 2` ...6  ...7 
+  <chr>  <chr>    <chr> <chr> <chr>    <chr> <chr>
+1 <NA>   Run 1    Run 2 Run 3 Run 1    Run 2 Run 3
+2 Max    22       23    24    25       26    27   
+3 Phoebe 34       34    32    34       51    12   
+4 Scamp  35       36    21    22       23    24   
+5 Chance 1234     1235  1236  1267     173   1233 
+6 Aimee  420      123   690   42       45    12   
+7 Kyle   22       23    25    26       67    54   
+
+
+
+
+

New Header Names

+
+

Step 1

+

First lets read back the data, this time however with some options. We will set the n_max equal to 2, to only read the first two rows, and set col_names to FALSE so we do not read the first row as headers.

+
+
ds1 <- read_excel(file_path, n_max = 2, col_names = FALSE)
+ds1
+
+
# A tibble: 2 × 7
+  ...1  ...2   ...3  ...4  ...5   ...6  ...7 
+  <chr> <chr>  <chr> <chr> <chr>  <chr> <chr>
+1 Name  Test 1 <NA>  <NA>  Test 2 <NA>  <NA> 
+2 <NA>  Run 1  Run 2 Run 3 Run 1  Run 2 Run 3
+
+
+
+
+

Step 2

+

Now that we have our headers lets first transpose them to a vertical matrix using the base function t(), then we will turn it back into a tibble to allow us to use tidyr fill function.

+
+
names <- ds1 %>%
+  t() %>%       #transpose to a matrix
+  as_tibble()   #back to tibble
+names
+
+
# A tibble: 7 × 2
+  V1     V2   
+  <chr>  <chr>
+1 Name   <NA> 
+2 Test 1 Run 1
+3 <NA>   Run 2
+4 <NA>   Run 3
+5 Test 2 Run 1
+6 <NA>   Run 2
+7 <NA>   Run 3
+
+
+

Note that tidyr fill can not work row wise, thus the need to flip the tibble so it is long vs wide.

+
+
+

Step 3

+

Now we use tidyr fill function to fill the NA’s with whatever value it finds above.

+
+
names <- names %>% fill(V1)  #use dplyr fill to fill in the NA's
+names
+
+
# A tibble: 7 × 2
+  V1     V2   
+  <chr>  <chr>
+1 Name   <NA> 
+2 Test 1 Run 1
+3 Test 1 Run 2
+4 Test 1 Run 3
+5 Test 2 Run 1
+6 Test 2 Run 2
+7 Test 2 Run 3
+
+
+
+
+

Step 4

+

This is where my data differed from many of the examples I could find online. Because the second row is also a header we can not just get rid of them. We can solve this using paste() combined with dplyr mutate to form a new column that combines the first and second column.

+
+
names <- names %>%
+  mutate(
+    new_names = paste(V1,V2, sep = "_")
+  )
+names
+
+
# A tibble: 7 × 3
+  V1     V2    new_names   
+  <chr>  <chr> <chr>       
+1 Name   <NA>  Name_NA     
+2 Test 1 Run 1 Test 1_Run 1
+3 Test 1 Run 2 Test 1_Run 2
+4 Test 1 Run 3 Test 1_Run 3
+5 Test 2 Run 1 Test 2_Run 1
+6 Test 2 Run 2 Test 2_Run 2
+7 Test 2 Run 3 Test 2_Run 3
+
+
+
+
+

Step 4a

+

One more small clean up task, in the example data the first column header Name, did not have a second label, this has created a name with an NA attached. We can use stringr to remove this NA.

+
+
names <- names %>% mutate(across(new_names, ~str_remove_all(.,"_NA")))
+names
+
+
# A tibble: 7 × 3
+  V1     V2    new_names   
+  <chr>  <chr> <chr>       
+1 Name   <NA>  Name        
+2 Test 1 Run 1 Test 1_Run 1
+3 Test 1 Run 2 Test 1_Run 2
+4 Test 1 Run 3 Test 1_Run 3
+5 Test 2 Run 1 Test 2_Run 1
+6 Test 2 Run 2 Test 2_Run 2
+7 Test 2 Run 3 Test 2_Run 3
+
+
+
+
+

Step 5

+

Now that are new name column is the way we want it, we can use dpylrs pull to return a vector of just that column

+
+
names <-  names %>% pull(new_names)
+
+
+
+
+

Final Data

+

Now that we have a vector of column names lets read in the original file using our new names. We set the skip argument to 2, to skip the first two rows, and set col_names equal to our vector of names. Note the last step I used the janitor package to provide names in snake case (the default for the clean names function.)

+
+
example_data <- readxl::read_excel(file_path, col_names = names, skip = 2) %>%
+  janitor::clean_names()
+example_data
+
+
# A tibble: 6 × 7
+  name   test_1_run_1 test_1_run_2 test_1_run_3 test_2_run_1 test_2_run_2
+  <chr>         <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
+1 Max              22           23           24           25           26
+2 Phoebe           34           34           32           34           51
+3 Scamp            35           36           21           22           23
+4 Chance         1234         1235         1236         1267          173
+5 Aimee           420          123          690           42           45
+6 Kyle             22           23           25           26           67
+# ℹ 1 more variable: test_2_run_3 <dbl>
+
+
+
+
+

Other Help

+

While searching for some solutions to my problem I found two good examples, however neither did exactly what I was trying to do.

+
    +
  1. This post by Lisa Deburine is pretty close to what I was trying to accomplish and gave me a good starting point. Read it here

  2. +
  3. This post by Alison Hill solves a simlar but slightly different problem. In her data the 2nd row is actually metadata not a second set of headers. Read it here

  4. +
+ + +
+ +

Reuse

Citation

BibTeX citation:
@online{belanger2020,
+  author = {Belanger, Kyle},
+  title = {Importing {Excel} {Data} with {Multiple} {Header} {Rows}},
+  date = {2020-06-22},
+  langid = {en}
+}
+
For attribution, please cite this work as:
+Belanger, Kyle. 2020. “Importing Excel Data with Multiple Header +Rows.” June 22, 2020. +
+ +
+ + + + \ No newline at end of file diff --git a/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png new file mode 100644 index 0000000..6fe0ec7 Binary files /dev/null and b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png differ diff --git a/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png new file mode 100644 index 0000000..0935940 Binary files /dev/null and b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png differ diff --git a/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html new file mode 100644 index 0000000..0b16df8 --- /dev/null +++ b/_site/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html @@ -0,0 +1,1050 @@ + + + + + + + + + + + +Kyle Belanger - Diabetes in Rural North Carolina : Data Collection and Cleaning + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+ +
+
+
+

Diabetes in Rural North Carolina : Data Collection and Cleaning

+

This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.

+
+
+ + +
+ +
+
Author
+ +
+ +
+
Published
+
+

July 25, 2020

+
+
+ + +
+ + +
+ + +
+ + + +
+ + + + +
+

Abstract

+

This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.

+
+
+

Data

+
+

Overall

+

Overall there are four data sources that have been used to create the analysis ready data for this project. There is one additional metadata file that contains the list of all county FIP codes, used for linking the various data sets. All data sets use the county FIPS as the county identifier, the county name is added at the end using the metadata. The image below shows the steps taken to achieve the analysis data set, as well as a table below showing the structure of each data set.

+

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Data Sources
DataStructureSourceNotes
2010 Census Rural/Urban Housingone row per countyUS CensusNA
County Health Rankingsone row per county, yearCounty Health RankingsRaw data is one year per file
Population Estimatesone row per county, year, age groupUS CensusNA
Diabetes Dataone row per county, yearCDC Diabetes AtlasRaw data is one year per file
+ + +
+
+
+
+

Rural Housing

+

The first data set comes from the US Census, and contains the amount of housing units inside both Urban and Rural areas. The raw data was taken and used to calculate the percentage of housing units in rural areas, as well as adding the classifications of Rural, Mostly Rural, and Mostly Urban. More about these classifications can be read here. This data set is from the 2010 US Census, which is then used to set the rural classification until the next Census (2020).

+

View greeter script here

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Rural Housing Data Set
County FipsPct RuralRural
0513120.41Mostly Urban
0513369.29Mostly Rural
0513577.84Mostly Rural
05137100.00Rural
0513955.07Mostly Rural
05141100.00Rural
Note:
Displaying 6 of 3,143 rows
+ + +
+
+
+
+

County Health Rankings

+

The second data set comes from County Health Rankings and contains data for the risk factors associated with diabetes, this data set is complied from many different data sources. The data was downloaded by year, and then combine to form one data set. County Health Rankings uses this data to rate health outcomes across all counties of the United States, for this analysis four categories have been extracted from the overall data set. Note that the food environment index is itself a combine measure, it is a score of both access to healthy food based on distance to grocery stores, as well as access based on cost.

+

View greeter script here

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
County Health Rankings Sources
MeasureData SourceFirst Year Available
Adult smokingBehavioral Risk Factor Surveillance System2010
Adult obesityCDC Diabetes Interactive Atlas2010
Physical inactivityCDC Diabetes Interactive Atlas2011
Food environment indexUSDA Food Environment Atlas, Map the Meal Gap2014
Source:
https://www.countyhealthrankings.org/explore-health-rankings/measures-data-sources/2020-measures
+ + +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
County Risk Factors Data Set
County FipsYearAdult Smoking PercentAdult Obesity PercentPhysical Inactivity PercentFood Environment Index
01001201028.130.0NANA
01003201023.124.5NANA
01005201022.736.4NANA
010072010NA31.7NANA
01009201023.431.5NANA
010112010NA37.3NANA
Note:
Displaying 6 of 34,555 rows
+ + +
+
+
+
+

Population Estimates

+

The third data set also comes from the US Census and contains population estimates for each county in the United States broken down by: year, age-group, sex, race, and ethnicity. For each row in the table the percent of each type of population was calculated using the yearly population total for the county. This breakdown is useful for this project as African-Americans and Hispanics suffer from diabetes at a higher rate then other groups.

+

View greeter script here

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
US Population Estimates Data Set
County FipsYearAge GroupYear Total PopulationTotal Male PopulationTotal Female PopulationWhite Male PopulationWhite Female PopulationBlack Male PopulationBlack Female PopulationAmerican Indian Male PopulationAmerican Indian Female PopulationAsian Male PopulationAsian Female PopulationNative Hawaiian Male PopulationNative Hawaiian Female PopulationNot Hispanic Male PopulationNot Hispanic Female PopulationHispanic Male PopulationHispanic Female PopulationPct Hsipanic Female PopulationPct MalePct FemalePct White Male PopulationPct White Female PopulationPct Black Male PopulationPct Black Female PopulationPct American Indian Male PopulationPct American Indian Female PopulationPct Asian Male PopulationPct Asian Female PopulationPct Native Hawaiian Male PopulationPct Native Hawaiian Female PopulationPct not Hispanic Male PopulationPct not Hispanic Female PopulationPct Hispanic Male Population
0100120100-4547731863171214151314356319321315001778165385590.113.403.132.582.400.650.580.010.000.020.030.000.003.253.020.16
0100120105-95477319841980150615173983691561522141916190868720.133.623.612.752.770.730.670.030.010.030.040.000.013.503.480.12
01001201010-1454773216321291657162142740913132319412098206465650.123.953.893.032.960.780.750.020.020.040.030.010.003.833.770.12
01001201015-195477321822047160115514974261362516422125199657510.093.983.742.922.830.910.780.020.010.050.030.010.003.883.640.10
01001201020-245477315731579122312193063166767321511153762420.082.872.882.232.230.560.580.010.010.010.010.010.002.762.810.11
01001201025-2954773157416171251123528934114923631505157069470.092.872.952.282.250.530.620.000.010.020.040.010.012.752.870.13
+ + +Note: + + Displaying 6 of 565560 rows + +
+ +
+
+
+
+

Diabetes Percentages

+

The final data set comes from the CDC Diabetes Atlas and contains the estimated prevalence of diabetes in each county of the United States, by year. The data set also includes the upper and lower estimated limits, see the previous post for an explanation of how these numbers are calculated. The data was downloaded by year, and then merged into one data set for the project.

+

View greeter script here

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
US Diabetes Data
YearCounty FipsDiabetes PercentageDiabetes Lower LimitDiabetes Upper Limit
20100100111.28.813.9
20100100310.28.711.9
20100100513.010.615.9
20100100710.68.213.3
20100100912.69.815.7
20100101116.112.420.4
+ + +
+
+
+
+
+
+

Analyis Data

+

After all data has been made ready the data is joined into a final analysis ready data set. Each row of this data set represents, one county, one year, and one age group. The data is also filtered to remove Alaska and Hawaii as these states could affect trends seen in the continental United States.

+

View the scribe script here

+

+ + +
+ +

Reuse

Citation

BibTeX citation:
@online{belanger2020,
+  author = {Belanger, Kyle},
+  title = {Diabetes in {Rural} {North} {Carolina} : {Data} {Collection}
+    and {Cleaning}},
+  date = {2020-07-25},
+  langid = {en}
+}
+
For attribution, please cite this work as:
+Belanger, Kyle. 2020. “Diabetes in Rural North Carolina : Data +Collection and Cleaning.” July 25, 2020. +
+ +
+ + + + \ No newline at end of file diff --git a/_site/search.json b/_site/search.json index da44963..8f97459 100644 --- a/_site/search.json +++ b/_site/search.json @@ -39,7 +39,7 @@ "href": "blog.html", "title": "Posts", "section": "", - "text": "Basic Exploration of WHO Tuberculosis Data\n\n\nToday I am going to dive into some real life data from the World Health Organization (WHO), exploring new and relapse cases of Tuberculosis. I clean up the data, and then make a few graphs to explore different variables.\n\n\n\n\n\n\n\n\n\nFeb 13, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nLine Graphs and Interactivity\n\n\nTableau for Healthcare Chapter 10. Static and Interactive examples\n\n\n\n\n\n\n\n\n\nFeb 10, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nFacets and a Lesson in Humility\n\n\nA look at Tableau for Healthcare Chapter 8. Table Lens graph.\n\n\n\n\n\n\n\n\n\nJan 29, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nMy Start to R\n\n\nA short introduction to my blog, and R journey.\n\n\n\n\n\n\n\n\n\nJan 24, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\nNo matching items" + "text": "Diabetes in Rural North Carolina : Data Collection and Cleaning\n\n\nThis is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.\n\n\n\n\n\n\n\n\n\nJul 25, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nImporting Excel Data with Multiple Header Rows\n\n\nA solution for importing Excel Data that contains two header rows.\n\n\n\n\n\n\n\n\n\nJun 22, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nBasic Exploration of WHO Tuberculosis Data\n\n\nToday I am going to dive into some real life data from the World Health Organization (WHO), exploring new and relapse cases of Tuberculosis. I clean up the data, and then make a few graphs to explore different variables.\n\n\n\n\n\n\n\n\n\nFeb 13, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nLine Graphs and Interactivity\n\n\nTableau for Healthcare Chapter 10. Static and Interactive examples\n\n\n\n\n\n\n\n\n\nFeb 10, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nFacets and a Lesson in Humility\n\n\nA look at Tableau for Healthcare Chapter 8. Table Lens graph.\n\n\n\n\n\n\n\n\n\nJan 29, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\n \n\n\n\n\nMy Start to R\n\n\nA short introduction to my blog, and R journey.\n\n\n\n\n\n\n\n\n\nJan 24, 2020\n\n\nKyle Belanger\n\n\n\n\n\n\nNo matching items" }, { "objectID": "posts/post-with-code/index.html", @@ -124,5 +124,54 @@ "title": "Line Graphs and Interactivity", "section": "plotly", "text": "plotly\nOne of the nice features of Tableau is the fact the graphs are interactive, while a good graph should speak for itself, end users love pretty things. I have been experimenting with Plotly, which has an open source package for R (as well as many other programming languages!). This example only just scratches the surface, but there will be many more to come!\n\ng2 <- ds1 %>% \n plot_ly(x = ~month, y = ~pct_tests_pos_for_influenza, type = \"scatter\", mode = \"lines\" \n ,color = ~fiscal_year\n ,colors = c(\"#a6611a\",\"#dfc27d\",\"#80cdc1\",\"#018571\")\n , hoverinfo = 'y') %>% \n layout(xaxis = list(\n title = \"\"\n )\n ,yaxis = list(\n title = \"% Tests (+) for Influenza\"\n )\n ,title = \"Flu Viral Surveillance: % Respiratory Specimens Positive for Influenza\"\n ,legend = list(\n x = 100\n ,y = 0.5\n ) \n \n )\n\ng2" + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "", + "text": "This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data." + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#overall", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#overall", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "Overall", + "text": "Overall\nOverall there are four data sources that have been used to create the analysis ready data for this project. There is one additional metadata file that contains the list of all county FIP codes, used for linking the various data sets. All data sets use the county FIPS as the county identifier, the county name is added at the end using the metadata. The image below shows the steps taken to achieve the analysis data set, as well as a table below showing the structure of each data set.\n\n\n\n\n\nData Sources\n\n\nData\nStructure\nSource\nNotes\n\n\n\n\n2010 Census Rural/Urban Housing\none row per county\nUS Census\nNA\n\n\nCounty Health Rankings\none row per county, year\nCounty Health Rankings\nRaw data is one year per file\n\n\nPopulation Estimates\none row per county, year, age group\nUS Census\nNA\n\n\nDiabetes Data\none row per county, year\nCDC Diabetes Atlas\nRaw data is one year per file" + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#rural-housing", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#rural-housing", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "Rural Housing", + "text": "Rural Housing\nThe first data set comes from the US Census, and contains the amount of housing units inside both Urban and Rural areas. The raw data was taken and used to calculate the percentage of housing units in rural areas, as well as adding the classifications of Rural, Mostly Rural, and Mostly Urban. More about these classifications can be read here. This data set is from the 2010 US Census, which is then used to set the rural classification until the next Census (2020).\nView greeter script here\n\n\n\nRural Housing Data Set\n\n\nCounty Fips\nPct Rural\nRural\n\n\n\n\n05131\n20.41\nMostly Urban\n\n\n05133\n69.29\nMostly Rural\n\n\n05135\n77.84\nMostly Rural\n\n\n05137\n100.00\nRural\n\n\n05139\n55.07\nMostly Rural\n\n\n05141\n100.00\nRural\n\n\n\nNote: \n\n\n\n\n Displaying 6 of 3,143 rows" + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#county-health-rankings", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#county-health-rankings", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "County Health Rankings", + "text": "County Health Rankings\nThe second data set comes from County Health Rankings and contains data for the risk factors associated with diabetes, this data set is complied from many different data sources. The data was downloaded by year, and then combine to form one data set. County Health Rankings uses this data to rate health outcomes across all counties of the United States, for this analysis four categories have been extracted from the overall data set. Note that the food environment index is itself a combine measure, it is a score of both access to healthy food based on distance to grocery stores, as well as access based on cost.\nView greeter script here\n\n\n\nCounty Health Rankings Sources\n\n\nMeasure\nData Source\nFirst Year Available\n\n\n\n\nAdult smoking\nBehavioral Risk Factor Surveillance System\n2010\n\n\nAdult obesity\nCDC Diabetes Interactive Atlas\n2010\n\n\nPhysical inactivity\nCDC Diabetes Interactive Atlas\n2011\n\n\nFood environment index\nUSDA Food Environment Atlas, Map the Meal Gap\n2014\n\n\n\nSource: \n\n\n\n\n https://www.countyhealthrankings.org/explore-health-rankings/measures-data-sources/2020-measures\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCounty Risk Factors Data Set\n\n\nCounty Fips\nYear\nAdult Smoking Percent\nAdult Obesity Percent\nPhysical Inactivity Percent\nFood Environment Index\n\n\n\n\n01001\n2010\n28.1\n30.0\nNA\nNA\n\n\n01003\n2010\n23.1\n24.5\nNA\nNA\n\n\n01005\n2010\n22.7\n36.4\nNA\nNA\n\n\n01007\n2010\nNA\n31.7\nNA\nNA\n\n\n01009\n2010\n23.4\n31.5\nNA\nNA\n\n\n01011\n2010\nNA\n37.3\nNA\nNA\n\n\n\nNote: \n\n\n\n\n\n\n\n Displaying 6 of 34,555 rows" + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#population-estimates", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#population-estimates", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "Population Estimates", + "text": "Population Estimates\nThe third data set also comes from the US Census and contains population estimates for each county in the United States broken down by: year, age-group, sex, race, and ethnicity. For each row in the table the percent of each type of population was calculated using the yearly population total for the county. This breakdown is useful for this project as African-Americans and Hispanics suffer from diabetes at a higher rate then other groups.\nView greeter script here\n\n\n\n\n\nUS Population Estimates Data Set\n\n\nCounty Fips\nYear\nAge Group\nYear Total Population\nTotal Male Population\nTotal Female Population\nWhite Male Population\nWhite Female Population\nBlack Male Population\nBlack Female Population\nAmerican Indian Male Population\nAmerican Indian Female Population\nAsian Male Population\nAsian Female Population\nNative Hawaiian Male Population\nNative Hawaiian Female Population\nNot Hispanic Male Population\nNot Hispanic Female Population\nHispanic Male Population\nHispanic Female Population\nPct Hsipanic Female Population\nPct Male\nPct Female\nPct White Male Population\nPct White Female Population\nPct Black Male Population\nPct Black Female Population\nPct American Indian Male Population\nPct American Indian Female Population\nPct Asian Male Population\nPct Asian Female Population\nPct Native Hawaiian Male Population\nPct Native Hawaiian Female Population\nPct not Hispanic Male Population\nPct not Hispanic Female Population\nPct Hispanic Male Population\n\n\n\n\n01001\n2010\n0-4\n54773\n1863\n1712\n1415\n1314\n356\n319\n3\n2\n13\n15\n0\n0\n1778\n1653\n85\n59\n0.11\n3.40\n3.13\n2.58\n2.40\n0.65\n0.58\n0.01\n0.00\n0.02\n0.03\n0.00\n0.00\n3.25\n3.02\n0.16\n\n\n01001\n2010\n5-9\n54773\n1984\n1980\n1506\n1517\n398\n369\n15\n6\n15\n22\n1\n4\n1916\n1908\n68\n72\n0.13\n3.62\n3.61\n2.75\n2.77\n0.73\n0.67\n0.03\n0.01\n0.03\n0.04\n0.00\n0.01\n3.50\n3.48\n0.12\n\n\n01001\n2010\n10-14\n54773\n2163\n2129\n1657\n1621\n427\n409\n13\n13\n23\n19\n4\n1\n2098\n2064\n65\n65\n0.12\n3.95\n3.89\n3.03\n2.96\n0.78\n0.75\n0.02\n0.02\n0.04\n0.03\n0.01\n0.00\n3.83\n3.77\n0.12\n\n\n01001\n2010\n15-19\n54773\n2182\n2047\n1601\n1551\n497\n426\n13\n6\n25\n16\n4\n2\n2125\n1996\n57\n51\n0.09\n3.98\n3.74\n2.92\n2.83\n0.91\n0.78\n0.02\n0.01\n0.05\n0.03\n0.01\n0.00\n3.88\n3.64\n0.10\n\n\n01001\n2010\n20-24\n54773\n1573\n1579\n1223\n1219\n306\n316\n6\n7\n6\n7\n3\n2\n1511\n1537\n62\n42\n0.08\n2.87\n2.88\n2.23\n2.23\n0.56\n0.58\n0.01\n0.01\n0.01\n0.01\n0.01\n0.00\n2.76\n2.81\n0.11\n\n\n01001\n2010\n25-29\n54773\n1574\n1617\n1251\n1235\n289\n341\n1\n4\n9\n23\n6\n3\n1505\n1570\n69\n47\n0.09\n2.87\n2.95\n2.28\n2.25\n0.53\n0.62\n0.00\n0.01\n0.02\n0.04\n0.01\n0.01\n2.75\n2.87\n0.13\n\n\n\n\n\nNote: \n\n Displaying 6 of 565560 rows" + }, + { + "objectID": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#diabetes-percentages", + "href": "posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html#diabetes-percentages", + "title": "Diabetes in Rural North Carolina : Data Collection and Cleaning", + "section": "Diabetes Percentages", + "text": "Diabetes Percentages\nThe final data set comes from the CDC Diabetes Atlas and contains the estimated prevalence of diabetes in each county of the United States, by year. The data set also includes the upper and lower estimated limits, see the previous post for an explanation of how these numbers are calculated. The data was downloaded by year, and then merged into one data set for the project.\nView greeter script here\n\n\n\nUS Diabetes Data\n\n\nYear\nCounty Fips\nDiabetes Percentage\nDiabetes Lower Limit\nDiabetes Upper Limit\n\n\n\n\n2010\n01001\n11.2\n8.8\n13.9\n\n\n2010\n01003\n10.2\n8.7\n11.9\n\n\n2010\n01005\n13.0\n10.6\n15.9\n\n\n2010\n01007\n10.6\n8.2\n13.3\n\n\n2010\n01009\n12.6\n9.8\n15.7\n\n\n2010\n01011\n16.1\n12.4\n20.4" + }, + { + "objectID": "posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html", + "href": "posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html", + "title": "Importing Excel Data with Multiple Header Rows", + "section": "", + "text": "Problem\nRecently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and then the second row listed a category within that group. Searching goggle I couldn’t really find a good example of what I was looking for, so I am putting it here in hopes of helping someone else!\n\n\nExample Data\nI have created a small Excel file to demonstrate what I am talking about. Download it here. This is the data from Excel. \n\n\nCheck Data\nFirst we will read the file in using the package readxl and view the data without doing anything special to it.\n\nlibrary(readxl) # load the readxl library\nlibrary(tidyverse) # load the tidyverse for manipulating the data\nfile_path <- \"example_data.xlsx\" # set the file path\nds0 <- read_excel(file_path) # read the file\nds0\n\n# A tibble: 7 × 7\n Name `Test 1` ...3 ...4 `Test 2` ...6 ...7 \n <chr> <chr> <chr> <chr> <chr> <chr> <chr>\n1 <NA> Run 1 Run 2 Run 3 Run 1 Run 2 Run 3\n2 Max 22 23 24 25 26 27 \n3 Phoebe 34 34 32 34 51 12 \n4 Scamp 35 36 21 22 23 24 \n5 Chance 1234 1235 1236 1267 173 1233 \n6 Aimee 420 123 690 42 45 12 \n7 Kyle 22 23 25 26 67 54 \n\n\n\n\nNew Header Names\n\nStep 1\nFirst lets read back the data, this time however with some options. We will set the n_max equal to 2, to only read the first two rows, and set col_names to FALSE so we do not read the first row as headers.\n\nds1 <- read_excel(file_path, n_max = 2, col_names = FALSE)\nds1\n\n# A tibble: 2 × 7\n ...1 ...2 ...3 ...4 ...5 ...6 ...7 \n <chr> <chr> <chr> <chr> <chr> <chr> <chr>\n1 Name Test 1 <NA> <NA> Test 2 <NA> <NA> \n2 <NA> Run 1 Run 2 Run 3 Run 1 Run 2 Run 3\n\n\n\n\nStep 2\nNow that we have our headers lets first transpose them to a vertical matrix using the base function t(), then we will turn it back into a tibble to allow us to use tidyr fill function.\n\nnames <- ds1 %>%\n t() %>% #transpose to a matrix\n as_tibble() #back to tibble\nnames\n\n# A tibble: 7 × 2\n V1 V2 \n <chr> <chr>\n1 Name <NA> \n2 Test 1 Run 1\n3 <NA> Run 2\n4 <NA> Run 3\n5 Test 2 Run 1\n6 <NA> Run 2\n7 <NA> Run 3\n\n\nNote that tidyr fill can not work row wise, thus the need to flip the tibble so it is long vs wide.\n\n\nStep 3\nNow we use tidyr fill function to fill the NA’s with whatever value it finds above.\n\nnames <- names %>% fill(V1) #use dplyr fill to fill in the NA's\nnames\n\n# A tibble: 7 × 2\n V1 V2 \n <chr> <chr>\n1 Name <NA> \n2 Test 1 Run 1\n3 Test 1 Run 2\n4 Test 1 Run 3\n5 Test 2 Run 1\n6 Test 2 Run 2\n7 Test 2 Run 3\n\n\n\n\nStep 4\nThis is where my data differed from many of the examples I could find online. Because the second row is also a header we can not just get rid of them. We can solve this using paste() combined with dplyr mutate to form a new column that combines the first and second column.\n\nnames <- names %>%\n mutate(\n new_names = paste(V1,V2, sep = \"_\")\n )\nnames\n\n# A tibble: 7 × 3\n V1 V2 new_names \n <chr> <chr> <chr> \n1 Name <NA> Name_NA \n2 Test 1 Run 1 Test 1_Run 1\n3 Test 1 Run 2 Test 1_Run 2\n4 Test 1 Run 3 Test 1_Run 3\n5 Test 2 Run 1 Test 2_Run 1\n6 Test 2 Run 2 Test 2_Run 2\n7 Test 2 Run 3 Test 2_Run 3\n\n\n\n\nStep 4a\nOne more small clean up task, in the example data the first column header Name, did not have a second label, this has created a name with an NA attached. We can use stringr to remove this NA.\n\nnames <- names %>% mutate(across(new_names, ~str_remove_all(.,\"_NA\")))\nnames\n\n# A tibble: 7 × 3\n V1 V2 new_names \n <chr> <chr> <chr> \n1 Name <NA> Name \n2 Test 1 Run 1 Test 1_Run 1\n3 Test 1 Run 2 Test 1_Run 2\n4 Test 1 Run 3 Test 1_Run 3\n5 Test 2 Run 1 Test 2_Run 1\n6 Test 2 Run 2 Test 2_Run 2\n7 Test 2 Run 3 Test 2_Run 3\n\n\n\n\nStep 5\nNow that are new name column is the way we want it, we can use dpylrs pull to return a vector of just that column\n\nnames <- names %>% pull(new_names)\n\n\n\n\nFinal Data\nNow that we have a vector of column names lets read in the original file using our new names. We set the skip argument to 2, to skip the first two rows, and set col_names equal to our vector of names. Note the last step I used the janitor package to provide names in snake case (the default for the clean names function.)\n\nexample_data <- readxl::read_excel(file_path, col_names = names, skip = 2) %>%\n janitor::clean_names()\nexample_data\n\n# A tibble: 6 × 7\n name test_1_run_1 test_1_run_2 test_1_run_3 test_2_run_1 test_2_run_2\n <chr> <dbl> <dbl> <dbl> <dbl> <dbl>\n1 Max 22 23 24 25 26\n2 Phoebe 34 34 32 34 51\n3 Scamp 35 36 21 22 23\n4 Chance 1234 1235 1236 1267 173\n5 Aimee 420 123 690 42 45\n6 Kyle 22 23 25 26 67\n# ℹ 1 more variable: test_2_run_3 <dbl>\n\n\n\n\nOther Help\nWhile searching for some solutions to my problem I found two good examples, however neither did exactly what I was trying to do.\n\nThis post by Lisa Deburine is pretty close to what I was trying to accomplish and gave me a good starting point. Read it here\nThis post by Alison Hill solves a simlar but slightly different problem. In her data the 2nd row is actually metadata not a second set of headers. Read it here\n\n\n\n\n\nReusehttps://creativecommons.org/licenses/by/4.0/CitationBibTeX citation:@online{belanger2020,\n author = {Belanger, Kyle},\n title = {Importing {Excel} {Data} with {Multiple} {Header} {Rows}},\n date = {2020-06-22},\n langid = {en}\n}\nFor attribution, please cite this work as:\nBelanger, Kyle. 2020. “Importing Excel Data with Multiple Header\nRows.” June 22, 2020." } ] \ No newline at end of file diff --git a/_site/site_libs/kePrint-0.0.1/kePrint.js b/_site/site_libs/kePrint-0.0.1/kePrint.js new file mode 100644 index 0000000..e6fbbfc --- /dev/null +++ b/_site/site_libs/kePrint-0.0.1/kePrint.js @@ -0,0 +1,8 @@ +$(document).ready(function(){ + if (typeof $('[data-toggle="tooltip"]').tooltip === 'function') { + $('[data-toggle="tooltip"]').tooltip(); + } + if ($('[data-toggle="popover"]').popover === 'function') { + $('[data-toggle="popover"]').popover(); + } +}); diff --git a/_site/site_libs/lightable-0.0.1/lightable.css b/_site/site_libs/lightable-0.0.1/lightable.css new file mode 100644 index 0000000..3be3be9 --- /dev/null +++ b/_site/site_libs/lightable-0.0.1/lightable.css @@ -0,0 +1,272 @@ +/*! + * lightable v0.0.1 + * Copyright 2020 Hao Zhu + * Licensed under MIT (https://github.com/haozhu233/kableExtra/blob/master/LICENSE) + */ + +.lightable-minimal { + border-collapse: separate; + border-spacing: 16px 1px; + width: 100%; + margin-bottom: 10px; +} + +.lightable-minimal td { + margin-left: 5px; + margin-right: 5px; +} + +.lightable-minimal th { + margin-left: 5px; + margin-right: 5px; +} + +.lightable-minimal thead tr:last-child th { + border-bottom: 2px solid #00000050; + empty-cells: hide; + +} + +.lightable-minimal tbody tr:first-child td { + padding-top: 0.5em; +} + +.lightable-minimal.lightable-hover tbody tr:hover { + background-color: #f5f5f5; +} + +.lightable-minimal.lightable-striped tbody tr:nth-child(even) { + background-color: #f5f5f5; +} + +.lightable-classic { + border-top: 0.16em solid #111111; + border-bottom: 0.16em solid #111111; + width: 100%; + margin-bottom: 10px; + margin: 10px 5px; +} + +.lightable-classic tfoot tr td { + border: 0; +} + +.lightable-classic tfoot tr:first-child td { + border-top: 0.14em solid #111111; +} + +.lightable-classic caption { + color: #222222; +} + +.lightable-classic td { + padding-left: 5px; + padding-right: 5px; + color: #222222; +} + +.lightable-classic th { + padding-left: 5px; + padding-right: 5px; + font-weight: normal; + color: #222222; +} + +.lightable-classic thead tr:last-child th { + border-bottom: 0.10em solid #111111; +} + +.lightable-classic.lightable-hover tbody tr:hover { + background-color: #F9EEC1; +} + +.lightable-classic.lightable-striped tbody tr:nth-child(even) { + background-color: #f5f5f5; +} + +.lightable-classic-2 { + border-top: 3px double #111111; + border-bottom: 3px double #111111; + width: 100%; + margin-bottom: 10px; +} + +.lightable-classic-2 tfoot tr td { + border: 0; +} + +.lightable-classic-2 tfoot tr:first-child td { + border-top: 3px double #111111; +} + +.lightable-classic-2 caption { + color: #222222; +} + +.lightable-classic-2 td { + padding-left: 5px; + padding-right: 5px; + color: #222222; +} + +.lightable-classic-2 th { + padding-left: 5px; + padding-right: 5px; + font-weight: normal; + color: #222222; +} + +.lightable-classic-2 tbody tr:last-child td { + border-bottom: 3px double #111111; +} + +.lightable-classic-2 thead tr:last-child th { + border-bottom: 1px solid #111111; +} + +.lightable-classic-2.lightable-hover tbody tr:hover { + background-color: #F9EEC1; +} + +.lightable-classic-2.lightable-striped tbody tr:nth-child(even) { + background-color: #f5f5f5; +} + +.lightable-material { + min-width: 100%; + white-space: nowrap; + table-layout: fixed; + font-family: Roboto, sans-serif; + border: 1px solid #EEE; + border-collapse: collapse; + margin-bottom: 10px; +} + +.lightable-material tfoot tr td { + border: 0; +} + +.lightable-material tfoot tr:first-child td { + border-top: 1px solid #EEE; +} + +.lightable-material th { + height: 56px; + padding-left: 16px; + padding-right: 16px; +} + +.lightable-material td { + height: 52px; + padding-left: 16px; + padding-right: 16px; + border-top: 1px solid #eeeeee; +} + +.lightable-material.lightable-hover tbody tr:hover { + background-color: #f5f5f5; +} + +.lightable-material.lightable-striped tbody tr:nth-child(even) { + background-color: #f5f5f5; +} + +.lightable-material.lightable-striped tbody td { + border: 0; +} + +.lightable-material.lightable-striped thead tr:last-child th { + border-bottom: 1px solid #ddd; +} + +.lightable-material-dark { + min-width: 100%; + white-space: nowrap; + table-layout: fixed; + font-family: Roboto, sans-serif; + border: 1px solid #FFFFFF12; + border-collapse: collapse; + margin-bottom: 10px; + background-color: #363640; +} + +.lightable-material-dark tfoot tr td { + border: 0; +} + +.lightable-material-dark tfoot tr:first-child td { + border-top: 1px solid #FFFFFF12; +} + +.lightable-material-dark th { + height: 56px; + padding-left: 16px; + padding-right: 16px; + color: #FFFFFF60; +} + +.lightable-material-dark td { + height: 52px; + padding-left: 16px; + padding-right: 16px; + color: #FFFFFF; + border-top: 1px solid #FFFFFF12; +} + +.lightable-material-dark.lightable-hover tbody tr:hover { + background-color: #FFFFFF12; +} + +.lightable-material-dark.lightable-striped tbody tr:nth-child(even) { + background-color: #FFFFFF12; +} + +.lightable-material-dark.lightable-striped tbody td { + border: 0; +} + +.lightable-material-dark.lightable-striped thead tr:last-child th { + border-bottom: 1px solid #FFFFFF12; +} + +.lightable-paper { + width: 100%; + margin-bottom: 10px; + color: #444; +} + +.lightable-paper tfoot tr td { + border: 0; +} + +.lightable-paper tfoot tr:first-child td { + border-top: 1px solid #00000020; +} + +.lightable-paper thead tr:last-child th { + color: #666; + vertical-align: bottom; + border-bottom: 1px solid #00000020; + line-height: 1.15em; + padding: 10px 5px; +} + +.lightable-paper td { + vertical-align: middle; + border-bottom: 1px solid #00000010; + line-height: 1.15em; + padding: 7px 5px; +} + +.lightable-paper.lightable-hover tbody tr:hover { + background-color: #F9EEC1; +} + +.lightable-paper.lightable-striped tbody tr:nth-child(even) { + background-color: #00000008; +} + +.lightable-paper.lightable-striped tbody td { + border: 0; +} + diff --git a/posts/2020-06-22_excel-data-multiple-headers/example_data.xlsx b/posts/2020-06-22_excel-data-multiple-headers/example_data.xlsx new file mode 100644 index 0000000..a9a1dbe Binary files /dev/null and b/posts/2020-06-22_excel-data-multiple-headers/example_data.xlsx differ diff --git a/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png b/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png new file mode 100644 index 0000000..58c7338 Binary files /dev/null and b/posts/2020-06-22_excel-data-multiple-headers/example_data_img1.png differ diff --git a/posts/2020-06-22_excel-data-multiple-headers/excel_headers.R b/posts/2020-06-22_excel-data-multiple-headers/excel_headers.R new file mode 100644 index 0000000..b43a2c5 --- /dev/null +++ b/posts/2020-06-22_excel-data-multiple-headers/excel_headers.R @@ -0,0 +1,47 @@ +#These first few lines run only when the file is run in RStudio, !!NOT when an Rmd/Rnw file calls it!! +rm(list=ls(all=TRUE)) #Clear the variables from previous runs. +cat("\f") # clear console + +# ---- example-data ---- + +library(readxl) # load the readxl library +library(tidyverse) # load the tidyverse for manipulating the data +file_path <- "example_data.xlsx" # set the file path +ds0 <- read_excel(file_path) # read the file +ds0 + + +# ---- fix-it-data ---- + +ds1 <- read_excel(file_path, n_max = 2, col_names = FALSE) +ds1 +# ---- fix-it-names ---- + +names <- ds1 %>% + t() %>% #transpose to a matrix + as_tibble() #back to tibble +names + +# ---- fix-it-names-1 ---- + +names <- names %>% fill(V1) #use dplyr fill to fill in the NA's +names +# ---- fix-it-names-2 ---- + +names <- names %>% + mutate( + new_names = paste(V1,V2, sep = "_") + ) +names + +# ---- fix-it-names-3 ---- +names <- names %>% mutate(across(new_names, ~str_remove_all(.,"_NA"))) +names +# ---- fix-it-names-4 ---- +names <- names %>% pull(new_names) +# ---- fix-it-final ---- + +example_data <- readxl::read_excel(file_path, col_names = names, skip = 2) %>% + janitor::clean_names() +example_data + diff --git a/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.qmd b/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.qmd new file mode 100644 index 0000000..2b2fb85 --- /dev/null +++ b/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.qmd @@ -0,0 +1,99 @@ +--- +title: "Importing Excel Data with Multiple Header Rows" +subtitle: | + A solution for importing Excel Data that contains two header rows. +date: 06-22-2020 +--- + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + + +```{r, echo=FALSE} + +knitr::read_chunk("excel_headers.R") +``` + +# Problem + +Recently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and then the second row listed a category within that group. Searching goggle I couldn't really find a good example of what I was looking for, so I am putting it here in hopes of helping someone else! + +# Example Data + +I have created a small Excel file to demonstrate what I am talking about. Download it [here](https://github.com/mmmmtoasty19/kyleb/tree/master/content/post/2020-06-15-importing-excel-data-with-multiple-headers/example_data.xlsx). This is the data from Excel. +![image of example data](example_data_img1.png) + +# Check Data + +First we will read the file in using the package readxl and view the data without doing anything special to it. + +```{r example-data} + +``` + +# New Header Names + +### Step 1 +First lets read back the data, this time however with some options. We will set the n_max equal to 2, to only read the first two rows, and set col_names to FALSE so we do not read the first row as headers. + +```{r fix-it-data } + +``` + +### Step 2 +Now that we have our headers lets first transpose them to a vertical matrix using the base function t(), then we will turn it back into a tibble to allow us to use tidyr fill function. + +```{r fix-it-names} + +``` + +Note that tidyr fill can not work row wise, thus the need to flip the tibble so it is long vs wide. + +### Step 3 + +Now we use tidyr fill function to fill the NA's with whatever value it finds above. + +```{r fix-it-names-1} + +``` + +### Step 4 + +This is where my data differed from many of the examples I could find online. Because the second row is also a header we can not just get rid of them. We can solve this using paste() combined with dplyr mutate to form a new column that combines the first and second column. + +```{r fix-it-names-2} + +``` + +### Step 4a + +One more small clean up task, in the example data the first column header Name, did not have a second label, this has created a name with an NA attached. We can use stringr to remove this NA. + +```{r fix-it-names-3} + +``` + +### Step 5 +Now that are new name column is the way we want it, we can use dpylrs pull to return a vector of just that column + +```{r fix-it-names-4} + +``` + +# Final Data + +Now that we have a vector of column names lets read in the original file using our new names. We set the skip argument to 2, to skip the first two rows, and set col_names equal to our vector of names. Note the last step I used the janitor package to provide names in snake case (the default for the clean names function.) + +```{r fix-it-final} + +``` + +# Other Help + +While searching for some solutions to my problem I found two good examples, however neither did exactly what I was trying to do. + +1. This post by Lisa Deburine is pretty close to what I was trying to accomplish and gave me a good starting point. Read it [here](https://debruine.github.io/posts/multi-row-headers/) + +2. This post by Alison Hill solves a simlar but slightly different problem. In her data the 2nd row is actually metadata not a second set of headers. Read it [here](https://alison.rbind.io/post/2018-02-23-read-multiple-header-rows/) diff --git a/posts/2020-07-25_diabetes-data-collection-and-cleaning/county_health_rankings_sources.csv b/posts/2020-07-25_diabetes-data-collection-and-cleaning/county_health_rankings_sources.csv new file mode 100644 index 0000000..24cfc2d --- /dev/null +++ b/posts/2020-07-25_diabetes-data-collection-and-cleaning/county_health_rankings_sources.csv @@ -0,0 +1,5 @@ +Measure,Data Source,First Year Available +Adult smoking,Behavioral Risk Factor Surveillance System,2010 +Adult obesity,CDC Diabetes Interactive Atlas,2010 +Physical inactivity,CDC Diabetes Interactive Atlas,2011 +Food environment index,"USDA Food Environment Atlas, Map the Meal Gap",2014 diff --git a/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png new file mode 100644 index 0000000..6fe0ec7 Binary files /dev/null and b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-cleaning.png differ diff --git a/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png new file mode 100644 index 0000000..0935940 Binary files /dev/null and b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-join.png differ diff --git a/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-sources-descrip.csv b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-sources-descrip.csv new file mode 100644 index 0000000..9bea89e --- /dev/null +++ b/posts/2020-07-25_diabetes-data-collection-and-cleaning/data-sources-descrip.csv @@ -0,0 +1,5 @@ +Data,Structure,Source,Notes +2010 Census Rural/Urban Housing,one row per county,US Census, +County Health Rankings,"one row per county, year",County Health Rankings,Raw data is one year per file +Population Estimates,"one row per county, year, age group",US Census, +Diabetes Data,"one row per county, year",CDC Diabetes Atlas,Raw data is one year per file diff --git a/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.qmd b/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.qmd new file mode 100644 index 0000000..e692d29 --- /dev/null +++ b/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.qmd @@ -0,0 +1,123 @@ +--- +title: "Diabetes in Rural North Carolina : Data Collection and Cleaning" +subtitle: | + This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data. +date: 07-25-2020 +--- + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = FALSE) +``` + + +# Abstract + +This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data. + +# Data + +## Overall + +Overall there are four data sources that have been used to create the analysis ready data for this project. There is one additional metadata file that contains the list of all county FIP codes, used for linking the various data sets. All data sets use the county FIPS as the county identifier, the county name is added at the end using the metadata. The image below shows the steps taken to achieve the analysis data set, as well as a table below showing the structure of each data set. + +![](data-cleaning.png) + +--- + +```{r} +library(magrittr) +data_sources <- readr::read_csv("data-sources-descrip.csv") +data_sources %>% knitr::kable(caption = "Data Sources") %>% kableExtra::kable_styling() + +``` + +## Rural Housing + +The first data set comes from the [US Census](https://data.census.gov/cedsci/table?q=rural%20house&hidePreview=true&tid=DECENNIALSF12010.H2&vintage=2010&tp=true&g=0100000US.050000), and contains the amount of housing units inside both Urban and Rural areas. The raw data was taken and used to calculate the percentage of housing units in rural areas, as well as adding the classifications of Rural, Mostly Rural, and Mostly Urban. More about these classifications can be read [here](https://www2.census.gov/geo/pdfs/reference/ua/Defining_Rural.pdf). This data set is from the 2010 US Census, which is then used to set the rural classification until the next Census (2020). + +View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-census-rural-housing.md) + +```{r} +ds_rural_housing <- readr::read_csv( + "https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/percentage-rural.csv") + +head(ds_rural_housing) %>% janitor::clean_names(case = "title") %>% + knitr::kable(align = 'c', caption = "Rural Housing Data Set") %>% + kableExtra::kable_styling(full_width = TRUE) %>% + kableExtra::footnote(general = "Displaying 6 of 3,143 rows") +``` + +## County Health Rankings + +The second data set comes from [County Health Rankings](https://www.countyhealthrankings.org/) and contains data for the risk factors associated with diabetes, this data set is complied from many different data sources. The data was downloaded by year, and then combine to form one data set. County Health Rankings uses this data to rate health outcomes across all counties of the United States, for this analysis four categories have been extracted from the overall data set. Note that the food environment index is itself a combine measure, it is a score of both access to healthy food based on distance to grocery stores, as well as access based on cost. + +View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-county-rankings-national.md) + +```{r} +county_risk_sources <- readr::read_csv("county_health_rankings_sources.csv") +county_risk_sources %>% knitr::kable(caption = "County Health Rankings Sources") %>% kableExtra::kable_styling() %>% + kableExtra::footnote(general = "https://www.countyhealthrankings.org/explore-health-rankings/measures-data-sources/2020-measures" , general_title = "Source: ") + +``` + +--- + +```{r} +ds_county_risk <- readr::read_csv( + "https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/national-diabetes-risk-factors-2010-2020.csv" + ) + +head(ds_county_risk) %>% janitor::clean_names(case = "title") %>% + knitr::kable(align = 'c', caption = "County Risk Factors Data Set") %>% + kableExtra::kable_styling() %>% + kableExtra::footnote(general = "Displaying 6 of 34,555 rows") +``` + +## Population Estimates + +The third data set also comes from the [US Census](https://www.census.gov/data/tables/time-series/demo/popest/2010s-counties-detail.html) and contains population estimates for each county in the United States broken down by: year, age-group, sex, race, and ethnicity. For each row in the table the percent of each type of population was calculated using the yearly population total for the county. This breakdown is useful for this project as African-Americans and Hispanics suffer from diabetes at a higher rate then other groups. + +View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-us-county-population-estimates.md) + + +```{r} +ds_population <- readr::read_csv( + "https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/us-county-population-estimates-v2.csv" + ) + +head(ds_population) %>% janitor::clean_names(case = "title") %>% + # dplyr::select(1:6) %>% + knitr::kable(caption = "US Population Estimates Data Set") %>% + kableExtra::kable_styling() %>% + kableExtra::scroll_box(width = "100%") %>% + kableExtra::footnote(general = "Displaying 6 of 565560 rows") + +``` + +## Diabetes Percentages + +The final data set comes from the [CDC Diabetes Atlas](https://www.cdc.gov/diabetes/data/index.html) and contains the estimated prevalence of diabetes in each county of the United States, by year. The data set also includes the upper and lower estimated limits, see the [previous post](https://kyleb.rbind.io/post/2020-06-25-diabetes-1/) for an explanation of how these numbers are calculated. The data was downloaded by year, and then merged into one data set for the project. + +View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-us-diabetes.md) + +```{r} +ds_diabetes <- readr::read_csv( + "https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/us-diabetes-data.csv" + ) + +head(ds_diabetes) %>% janitor::clean_names(case = "title") %>% + knitr::kable(caption = "US Diabetes Data") %>% + kableExtra::kable_styling() + +``` + +--- + +# Analyis Data + +After all data has been made ready the data is joined into a final analysis ready data set. Each row of this data set represents, one county, one year, and one age group. The data is also filtered to remove Alaska and Hawaii as these states could affect trends seen in the continental United States. + +View the scribe script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/1-scribe-diabetes-data-set.md) + +![](data-join.png)