DSCI 100
Where are we? Where are we going?
image source: R for Data Science by Grolemund & Wickham
The cartoon illustrations in this slide deck are all created by Allison Horst
Illustrations from the Openscapes blog Tidy Data for reproducibility, efficiency, and collaboration by Julia Lowndes and Allison Horst”
Notes:
Tuberculosis data
Let’s assume the goal is to analyze the rates of tuberculosis, and perhaps consider population size in the analysis.
True (A) or False (B): This data is tidy.
| country | year | rate |
|---|---|---|
| Afghanistan | 1999 | 745/19987071 |
| Afghanistan | 2000 | 2666/20595360 |
| Brazil | 1999 | 37737/172006362 |
| Brazil | 2000 | 80488/174504898 |
| China | 1999 | 212258/1272915272 |
| China | 2000 | 213766/1280428583 |
Notes:
True(A) or False(B): This data is tidy.
| country | cases (year=1999) | cases (year=2000) | population (year=1999) | population (year=2000) |
|---|---|---|---|---|
| Afghanistan | 745 | 2666 | 19987071 | 20595360 |
| Brazil | 37737 | 80488 | 172006362 | 174504898 |
| China | 212258 | 213766 | 1272915272 | 1280428583 |
Notes:
True(A) or False(B): This data is tidy.
| country | 1999 | 2000 |
|---|---|---|
| Afghanistan | 745 | 2666 |
| Brazil | 37737 | 80488 |
| China | 212258 | 213766 |
Notes:
True(A) or False(B): This data is tidy.
| country | year | key | value |
|---|---|---|---|
| Afghanistan | 1999 | cases | 745 |
| Afghanistan | 1999 | population | 19987071 |
| Afghanistan | 2000 | cases | 2666 |
| Afghanistan | 2000 | population | 20595360 |
| Brazil | 1999 | cases | 37737 |
| Brazil | 1999 | population | 172006362 |
| Brazil | 2000 | cases | 80488 |
| Brazil | 2000 | population | 174504898 |
| China | 1999 | cases | 212258 |
| China | 1999 | population | 1272915272 |
| China | 2000 | cases | 213766 |
| China | 2000 | population | 1280428583 |
Notes:
True(A) or False(B): This data is tidy.
| country | year | cases | population |
|---|---|---|---|
| Afghanistan | 1999 | 745 | 19987071 |
| Afghanistan | 2000 | 2666 | 20595360 |
| Brazil | 1999 | 37737 | 172006362 |
| Brazil | 2000 | 80488 | 174504898 |
| China | 1999 | 212258 | 1272915272 |
| China | 2000 | 213766 | 1280428583 |
Notes:
tidyverse package functions from:
dplyr package (select, filter, mutate, group_by, summarize)
tidyr package (pivot_longer, pivot_wider)
purrr package (map_dfr)
The penguins data set comtains data on 344 penguins. There are 3 different species of penguins in this data set, collected from 3 islands in the Antarctica.
select One ColumnThe select function is used to select a subset of columns (variables) from a dataframe.
select Many Columnsselect, and Assign to an Objectselect All But Specific Columnsselect All Columns That Start With “bill_”filter with One ConditionThe filter function is used to choose a subset of rows (observations) in a dataframe.
Example: filter for only penguins with flippers longer than 190 mm
filter with Two ConditionsExample: filter for Chinstrap penguins with flippers longer than 190 mm
filter with Two Conditions, Assign to ObjectWe can also assign the filtered data set to a new object to reference later!
mutateThe mutate function transforms old columns to add new columns.
Example: convert body mass from grams to pounds
mutate and Assign to ObjectThe mutate function does not automatically overwrite the original data frame. So, we can save it as a new one. Note that the body_mass_g column still exists! We could drop it later using select.
When you need type out a long sequence of operations on data, you could either:
|>Let’s look closer at each one of these
Disadvantages:
penguins_1 and penguins_2 objects are important for some reason, while they are just temporary intermediate computations.penguins_1 and penguins_2 are used in each subsequent line.Disadvantage:
mutate above)You can also pipe with the |> symbol: passes the output of a function (or a data set) to the 1st argument of another.
Notes:
|> at the end of a line, it keeps reading the next line of code before evaluating!Advantages
Try to fill in the code below that looks at the mtcars data set and:
Creates a new variable called wt_lbs (hint: the variable wt is currently written in thousands of lbs. Simply multiply wt by 1000)
Remove cars that have less than 20 mpg
Keeps only the mpg, cyl, wt_lbs, and hp columns.
group_by + summarizeFor example computing the average particle pollution per city (source https://info201.github.io/dplyr.html):
group_by + summarizeAnother example: splitting the penguins data into one group per species, and then summarizing the values within each group e.g. reporting the average body_mass per species.
To do this we use group_by + summarize to iterate over species, calculating average body mass.
Iteration is when you need to do something repeatedly (e.g., ringing in and bagging groceries at the till)
In data science, it’s doing the same thing over and over, too
important to reduce duplication of code
easier to see the intent of your code
likely have fewer bugs
map_dfrmap_dfr iterates over columns, e.g. to calculate the average for each numeric column in the penguins data
Be careful: There are a lot of map_... functions that you could use (map, map_lgl, map_chr, etc). Usually in this course we’ll only use map_dfr, but you should investigate the others yourself!
Let’s calculate the average of numeric column in penguins data set
If not, let’s work together on this week’s worksheet.