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...

1 answer below »

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.




  1. (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.




  2. (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.




  3. (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.




  4. (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.




  5. (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.




  6. (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.




  1. (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).




  2. (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).




  3. (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.




  4. (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?




  1. (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."




  2. (b) [1 point] Write a function callF2Cthat can convert temperature in Fahrenheit into Celsius.




  3. (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.



Answered 2 days AfterOct 07, 2021

Answer To: HIA 226 Assignment 04 Page 2 of 5R competency The “R competency” questions cover some of what we...

Mohd answered on Oct 10 2021
123 Votes
-
-
-
10/8/2021
Loading Packages
library(readr)
library(magrittr)
library(dplyr)
library(ggplot2)
library(rmarkdown)
library(readr)
brfss2018 <- read_csv("data/brfss2018.csv")
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), andbmi XXXXXXXXXXYou 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.
1. [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.
brfss2018%>%
filter(state==c(1:6))
## # A tibble: 5,788 x 5
## state sleptim1 sex1 ment14d bmi5
##
## 1 1 7 2 1 2231
## 2 1 8 2 1 2496
## 3 1 6 2 2 2834
## 4 1 6 2 1 2661
## 5 1 6 2 1 2249
## 6 1 8 2 1 2707
## 7 1 8 2 1 2506
## 8 1 6 2 1 4826
## 9 1 8 1 1 4069
## 10 1 7 2 1 2744
## # ... with 5,778 more rows
1. [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.
brfss2018$sex1<-replace(brfss2018$sex1,brfss2018$sex1>5,NA)
table(brfss2018$sex1)
##
## 1 2
## 197412 238911
1. [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.
brfss2018$sex1<-as.factor(brfss2018$sex1)
levels(brfss2018$sex1) <-c("Male", "Female")
table(brfss2018$sex1)
##
## Male Female
## 197412 238911
brfss2018%>%
count(sex1)
## # A tibble: 3 x 2
## sex1 n
##
## 1 Male 197412
## 2 Female 238911
## 3 ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here