HIA 226 Assignment 04 Page 2 of 5R competency
The “R competency” questions cover some of what we covered in the video lectures. You may use this as a test of your comprehension.
1. You will need to first download and unzip theWA04Projfile for this assignment.
In the project folder, you will find a data filebrfss2018.csvin the Data folder. This is a subset of the 2018 Behavioral Risk Factor Surveillance System. An accompanying PDF code book can also be found in the Document folder; you may need to refer to the code book later.
The variables (and page # of the code book on which you can learn more about the variable) arestate(2–4),sleptim1(21),sex1(29),ment14d(126), andbmi5(141). You may find that some variable names originally come with a leading underscore ( ), which I removed for convenience (R tends to dislike variable with leading underscore.)
Complete the following tasks. Provide the R codes you used whenever appropriate. You may use either Base R functions, tidyverse functions, or a combination of both.
(a) [1 point] Create a subset callednewhich contains only data from the six states in New England. (If you struggle with this part, see the code file “Hints for multiple conditions” for some inspiration.) Use an appropriate R command to find out how many cases (rows) are there inne.
(b) [1 point] Recode those who answered “Don’t know/Not Sure” or refused the answer the question about their sex into missing. Usingtable()or similar technique, show that you have recoded those “Don’t know/Not Sure” successfully.
(c) [1 point] Convertsex1into a factor variable, and properly label the values as “Male” and “Female.” Provide a table to show the total males and females in the data.
(d) [1 point] Convertment14dinto a factor variable, and properly label the values as “0 days”, “1-13 days”, “14+ days”, and “Don’t know/Refused/Missing”. Provide a table to show the total counts in each category.
(e) [2 points] Using the table1 package, create the following table (Numbers were blurred by me):
Figure 1: Table 1 for you to create
Export your version of the table as a .png file and insert that into the assignment.
(f) [1 point] Provide the R codes that you used to create the above table.
HIA 226: Data Wrangling
HIA 226 Assignment 04 Page 3 of 5 2. In this question we will continue to use thenedata set created in the previous question.
(a) [1 point] It may be quite unlikely that someone sleep for more than 20 hours per day on average. Because of that, recode the sleep hours that are bigger than 20 into missing (NA).
(b) [2 points] Using theaggregate()function, create a data set calledmeanSleepwhich contains the mean hours of sleep per 24 hours, broken down by sex (sex1) and the factor version of days feeling mentally unwell (ment14d). Paste the aggregated data set with your R codes into the assignment. There should be 8 lines of data (2 sexes×4 levels of mental health days).
(c) [1 point] In no more than two sentences, describe the association between sleep time and feeling mentally unwell, and if such association appears to be different between sexes.
(d) [0 points]FREE CHALLENGE I:Refer to the first example onthis web page, create a grouped bar chart to show the 8 data points inmeanSleep. Here is a very rough sketch for your reference:
Figure 2: A very drafty one as an example
HIA 226: Data Wrangling
HIA 226 Assignment 04 Page 4 of 5
3. In this session we will learn how to write a simple function. Function is simply an input-output machine which we can provide some ingredients for some outputs. For example, here is a function that convert inches to cm:
in2cm
}
The function namein2cmis customizable, you can call it anything. Thefunction(), followed by a pair of curly brackets ({and}) defines the main body of the function. The two lines inside the curly brackets define the actions to be taken.x1is the input, andx2is the output. We assign the output to bex1 * 2.54, and then use areturn()to display the answer. Give it a try by submitting this function, then use the following line to check how many centimeters are there in 15 inches:
in2cm(15)
Fun, right?
(a) [2 points] Try to combine thepaste()statement with thereturn()command, so that when we submit 15 inches into the function, we will get a statement:
"15 in is equal to 38.1 cm."
(b) [1 point] Write a function callF2Cthat can convert temperature in Fahrenheit into Celsius.
(c) [0 points]FREE CHALLENGE II:Push yourself a bit further, try to reinvent the tem- perature conversion function above (let’s call it F2C2F) so that if you type in:F2C2F(25, "C")
you’ll get the answer in F. And if you type in:
F2C2F(55, "F")
you’ll get the answer in C. You may want to refer to the hungryDragon example for some inspiration/hint.