Chapter 9 Appendix: Exercise Solutions

require(dplyr)

9.1 Tutorial 2: Introduction to R

This notebook contains the solutions for all of the Try it yourself sections of tutorial 1: Introduction to R. Make sure that you have at least tried to solve these sections first before viewing this notebook.

2.1

  1. Can you replicate and solve these problems in R?
  • 2^2
  • 2 × 2
  • 2 + 5 × (5 ÷ 4)^6
  • what is the remainder of 52 ÷ 5
  • what is the whole number solution to 82 ÷ 8
2^2
## [1] 4
2 * 2
## [1] 4
2 + 5 * (5 / 4)^6
## [1] 21.07349
52 %/% 5
## [1] 10
82 %% 8
## [1] 2
  1. Can you solve for x using R?

a <- 9 + 3 * 6

x <- a ÷ 2

a <- 9 + 3 * 6
x <- a / 2
x
## [1] 13.5
## x is equal to 13.5!

2.2

Translate the following into R and find the output: * 8 times 3 is greater than 8? * eleven divided by seven is not equal to 2? * 9 is less than or equal to 18?

8 * 3 > 8
## [1] TRUE
11 / 7 != 2
## [1] TRUE
9 <= 18
## [1] TRUE

2.3

Can you try storing a string? Assign the string “hello world, I am here” to the variable named start.

start <- "hello world, I am here"
start
## [1] "hello world, I am here"

Note how the string is in "" but the variable name is not. Why do you think this is? Because start is now a known variable that stores a string. In other words, start does not mean “start,” instead it means “hello world, I am here.”

2.4

It is important to note that R is case-sensitive. This means that it distinguishes capitalized from non-capitalized characters, so logical and Logical are read as two separate things by R!

Try typing Logical with a capitalized “L.” How does R respond to this?

#Logical
    ### an error message that says "object 'Logical' not found" pops up

2.5

Why do you think numeric, character, and logical are not in "" but Number, Text, and T/F are?

Because numeric, character, and logical are known variables that hold meanings (and that we already defined), while Number, Text, and T/F are not. They are actually just texts that we use to rename our column names - they do not hold any meanings.

2.6

  1. What are 2 ways that we can print rows 1 to 5 of the data frame faithful?
## 1. print(faithful[1:12, ])

## 2. faithful[1:12, ]
  1. What is the value of the cell in the fourth row and second column of the data frame faithful?
## faithful[4,2]

2.7

Write a code to find the structure of the variable waiting in the faithful dataset.

## str(faithful$waiting)

2.8

Remember those variables that we created earlier in the tutorial? Try finding the lengths of data frame and numeric.

Challenge: Psst! There are actually 2 ways for you to find the length of numeric.

## length(dataframe)

## length(numeric) #OR
## length(dataframe$numeric)

2.9

Recall that in order for us to refer to a variable in a dataset, we need to first type the dataset name following by a $ before we can type the variable name.

A way to avoid repeating faithful$ everytime is to attach the dataset using attach(faithful).

Try attaching the dataset faithful then find the mean of the variable eruptions without using $!

## attach(faithful)
## mean(eruptions)

9.2 Tutorial 3: Importing Data into R with readr

This notebook contains the solutions for all of the Try it yourself sections of tutorial 2: Importing Data into R with readr. Make sure that you have at least tried to solve these sections first before viewing this notebook.

3.1

Can you try importing the bpx.csv file into R using the function read_csv()?

#read_csv("../input/import/bpx.csv")

3.2

Can you identify the mistakes of the following codes?

a. The pathway is not in ""

b. “DEMO” should not be capitalized

c. “Read_csv” should not be capitalized

d. The entire pathway needs to be in "" instead of just the file name

3.3

Just by looking at the actual data frame, can you guess what type of data col_double() and col_character() are?

(HINT: doubles? integers? logical? character?)

Here is a list of col_x() and what they mean: * col_double() – Doubles * col_integer() – Integers * col_logical() – True/False * col_date() – Date * col_time() – Time * col_datetime – Date and Time * col_character() – Text, Character, and everything else

This list is not extensive, so you can use ?cols to learn more about column specification!

3.4

You may also notice that the header of Skip_2 is incorrect. This is because R recognizes the header of our data as the first row, thus omiting it when importing demo.csv into R.

Let’s say this is not what we really want. What we actually want to do is to remove the first two rows of actual data while keeping the header. What do you think we have to do to achieve this?

(HINT: Recall what we learn about extracting rows in tutorial 1)

#DEMO[3:10, ]

3.5

Import the bpx.xlsx into R using the read_excel() function.

#read_excel("../input/import/bpx.xlsx")

9.3 Tutorial 4: Introduction to NHANES

This notebook contains the solutions for all of the Try it yourself sections of Tutorial 3: Introduction to NHANES. Make sure that you have at least tried to solve these sections first before viewing this notebook.

4.1

  1. Find all the Examination Data in survey cycle 2013-2014
## nhanesTables('EXAM', 2013)
  1. Import the blood pressure dataset in the Examination Data in survey cycle 2013-2014
## bpx <- nhanes('BPX_H')
  1. Translate the following variables in the BPX dataset
  • BPXPULS - Pulse regular or irregular?

  • BPAARM - Arm selected

## bpxtranslate <- nhanesTranslate('BPX_H',  c('BPXPULS', 'BPAARM'), data = bpx)

9.4 Tutorial 5: Data Analysis with dplyr

This notebook contains the solutions for all of the Try it yourself sections of Tutorial 4: Data Analysis with dplyr. Make sure that you have at least tried to solve these sections first before viewing this notebook.

5.1

In the bpx dataframe, keep the following variables and top 5 rows only:

  • SEQN
  • PEASCST1
  • PEASCTM1
  • BPXSY1
  • BPXDI1
## new_bpx <- select(bpx,
##                    c(SEQN,
##                     PEASCST1,
##                     PEASCTM1,
##                     BPXSY1,
##                     BPXDI1))
## new_bpx <- head(new_bpx,5)

5.2

  • SEQN -> id
  • PEASCST1 -> bp_status
  • PEASCTM1 -> bpt_sec
  • BPXSY1 -> systolic
  • BPXDI1 -> diastolic
## new_bpx <- rename(bpx,
##                    id = SEQN,
##                    bp_status = PEASCST1,
##                    bpt_sec = PEASCTM1,
##                    systolic = BPXSY1,
##                    diastolic = BPXDI1)

5.3

In the demo dataframe, find all the records that:

  1. he participant who is a male
## filter(final_demo, gender == "Male")
  1. the participant who is a male and is older tha 50 years old
## filter(final_demo, gender == 'Male' && age > 50)
  1. the education level is missing
## filter(final_demo, is.na(edu))

5.4

Re-order the rows in the bpx dataset by Blood Pressure Time in Seconds (bpt_sec) in descending order:

## arrange(final_bpx,desc(bpt_sec))

5.5

  1. Create a new variable called called rescale_bpt_sec that records the Blood Pressure Time in miuntes. Keep both original and new variables.
##  mutate(final_bpx,rescale_bpt_sec = bpt_sec/60)
  1. Create rescale_bpt_sec in the same way above and only keep new variables.

Note: Try to avoid using select().

##  transmute(final_bpx,rescale_bpt_sec = bpt_sec/60)

5.6

Find the average age in the demo dataframe per education level

##  by_edu <- group_by(edu)

## summarize(by_edu, average_age = mean(age, na.rm = TRUE))

5.7

Return the observations which has more than 3 records in each gender group.

## by_gender <-group_by(final_demo,gender)

## filter(by_edu,n()>3)

5.8

Compute the difference in each age and the average mean in each education level group

## by_edu <-group_by(final_demo,edu)

## mutate(by_edu, diff_age = age - mean(age, na.rm = T))

5.9

Re-write the following code using pipe operator

## temp <- filter(final_bpx, systolic > 120)
## temp <- mutate(temp, bpt_min = bpt_sec/60)
## temp
## final_bpx %>%
##    filter(systolic > 120) %>%
##    mutate(bpt_min = bpt_sec/60)

9.5 Tutorial 6: Data Visualization with ggplot2

This notebook contains the solutions for all of the Try it yourself sections of tutorial 5: Data Visualization with ggplot. Make sure that you have at least tried to solve these sections first before viewing this notebook.

6.1

Plot a scatterplot to show the relationship between Diastolic Blood Pressure (x-axis) and Blood Pressure Time in Seconds (y-axis).

## ggplot(data = demo_bpx) +
##   geom_point(aes(x = Diastolic, 
##                  y = BPT), 
##              na.rm = TRUE)

6.2

Plot a scatterplot using the Diastolic (x-axis) and Systolic (y-axis) variables in the data frame demo_bpx where all of the data points are blue.

## ggplot(demo_bpx) +
##   geom_point(aes(x = Systolic, 
##                  y = Diastolic), 
##              color = "blue",
##              na.rm = TRUE)

6.3

Try increasing and decreasing the binwidth of a frequency polygon. What differences do you see? What does binwidth actually mean?

Higher binwidths give us less fluctuations than lower binwidths. When we are increasing the binwidths, we are actually increasing the length of intervals, which leads to less intervals. In other words, each data point are a bit further away from each other, so when connected, the graph looks less detailed and smoother.

6.4

Try recreating the graph above but without any missing (NA) values.

HINT: Filter out any information we do not need using logical operators!

First, we need to filter out all of the NA values:

## no_NA <- filter(demo_bpx, Race != "NA" & Gender != "NA")

Then, we can graph our facets normally:

## ggplot(no_NA) +
##   geom_point(aes(x = Diastolic, y = Systolic), na.rm = TRUE) +
##   facet_grid(Race ~ Gender)

9.6 Tutorial 7: Date and Time Data with lubridate

This notebook contains the solutions for all of the Try it yourself sections of tutorial 6: Date & Time Data with lubridate. Make sure that you have at least tried to solve these sections first before viewing this notebook.

7.1

After running the today() and now() codes above, what do you see?

Try to also change the time zone to where you are or to something else. Now what do you see when you run today() and now()?

After running today() and now(), you should see that today() is a date data and now() is a datetime data.

Changing the time zone means that the date and time will be different if we run today() and now() again.

7.2

Try creating a new column named “Day_visit” that only contains information of the Year, Month, and Day columns using the Friends_visits dataframe that we just created.

## head(
##   Friends_visits %>% 
##   mutate(Hour_visit = make_datetime(Year, Month, Day))
##    )

7.3

Using the functions introduced above, solve the following questions: 1. What day of the week is April 2, 2014? 2. What day of the year is 2017-09-15? 3. What day of the month is 20190830? 4. Find the months of the last 11 records of the column Visit_hour.

#1. wday(mdy("April 2, 2014"), label = TRUE)

#2. yday(ymd("2017-09-05"))

#3. mday(ymd(20190830))

#4. tail(month(Friends_visits$Visit_time, label = TRUE), 11)

7.4

Using the same Friends_visit dataset, create a similar graph as above but the x-axis is days of the week. In other words, create a bar graph that shows how many visits there are in each day of the week.

## Friends_visits %>%
##  mutate(Week_day = wday(Visit_time, label = TRUE, abbr = FALSE)) %>%
##  ggplot(aes(Week_day)) +
##  geom_bar()

7.5

Try to update our month to 13. What happened to our date/time? What is the output?

# DT %>%
#     update(month = 13)

## The year turns to 2021 because a year only has 12 months!

7.6

Recreate the frequency polygon above but change the binwidth so that all flights within each 30 minutes are clumped into one single data point. How do the graphs differ? Can you think of a few scenarios where one would be preferred?

## Friends_visits %>%
##    mutate(Visit_hour = update(Visit_time, yday = 1)) %>%
##    ggplot(aes(Visit_hour)) +
##    geom_freqpoly(binwidth = 900)

9.7 Tutorial 8: Data Summary with tableone

This notebook contains the solutions for all of the Try it yourself sections of tutorial 7: Data Summary with tableone. Make sure that you have at least tried to solve these sections first before viewing this notebook.

8.1

Challenge: Why do you think we need to change the names of our variables AFTER we translate them?

Hint: Think about the data = demo argument in nhanesTranslate()

Because after we use the function names() our data is no longer recognizable by R as being connected to the DEMO_H dataset that we downloaded from NHANES.

8.2

Create a tableone without the vars argument. What do you see? Do you think the vars argument is necessary in our case? If not, in what situation(s) do you think it would be necessary?

The vars argument tells R which variables you want to include in your tableone. If it is missing from CreateTableOne(), this means that we want R to select ALL variables of the original table in our tablone. Therefore, in our case, vars is not neccesary because we are selecting all variables anyway. But in cases where we only want to select some variables to include in our tableone, vars is an absolute must!

8.3

How do you know if a variable is nonnormal? Try using the function summary() and look at the number under skew. How do you decide if something is normal or nonnormal? Is the decision to make “Age” nonnormal accurate?

After you plug the dataset name into summary(), you should see a numerical value below “skew” that tells us the skewness of the data. The further it is from 0 (both negative and positive), the further it is from normal! The decision to make “Age” nonnormal is arguably not quite accurate because the skewness of “Age” is only 0.4.

8.4

Create a tableone using the demo_translate dataset. Keep all variables and stratified the data using “Age.” What do you see? Do you think this is a helpful tableone?

## CreateTableOne(data = demo_translate,
##               vars = c("Race", "Education", "Gender"),
##               factorVars = c("Race","Education", "Gender"),
##               strata = "Age"
##              )

Just like how we should not use continuous variables to create facets (check ggplot tutorial), we should also not stratified our data using continuous variables. When we stratify our data using numerical continuous variables, the tableone becomes too big to handle. The information that we get is also not meaningful as there is a lot of values including lots of NA values. If we want to stratify our data by age, it may be better to use age groups instead of single age values.