--- title: "Derived Variables" author: "JJB + Course" date: "09/10/2018" output: html_document: toc: true toc_float: collapse: false --- ```{r setup, include=FALSE} knitr::opts_chunk\$set(echo = TRUE) ``` ## Example: Blood Pressure Data frame containing the blood pressure data ```{r construct-bp-data} ## Construct BP data bp_data = data.frame( Subject_ID = c("S005", "S130", "S023", "S098", "S035", "S007", "S104"), Sex = c("Male", "Female", "Male", "Male", "Male", "Female", "Female"), Systolic = c(110, 141, 125, 168, 115, 122, 135) ) ## View data.frame bp_data ``` ```{r augment-data-frame-with-type} ## Add column to data.frame bp_data\$BP_Type = c("Normal", "Stage 2", "Elevated", "Stage 2", "Normal", "Elevated", "Stage 1") ``` ## Example: Logical Values ```{r} # Values equivalent to TRUE TRUE T 1L # Values equivalent to FALSE FALSE F 0L ``` ## Example: Performing Comparisons ```{r comparisons} 42 == 42 # Is 42 equal to 42? Yes or No? 42 != 32 # Is 42 not equal to 32? Yes or No? 12 < 38 # Is 12 less than 38? Yes or No? 12 > 38 # Is 12 greater than 38? Yes or No? 38 > 38 # Is 38 greater than 38? Yes or No? 38 >= 38 # Is 38 greater than or equal to 38? Yes or No? c(3, 2, 1, -5) > 2 # Are values greater than 2? Yes or No? ``` ### Example: Vectorized Comparison ```{r ex-block-comparison} # Comparison of vectors c(11, -3, 1, 2) >= c(5, -2, 9, 42) # Comparison of vector # to a single element c(3, 2, 1, -5) < 2 ``` ### Example: Set Comparison "Needle in the Haystack" problems ```{r} 2 %in% c(3, 2, 1, -5) # Does 2 belong to any values? Yes or No? 2 %in% c(3, 1, -5) # Does 2 belong to any values? Yes or No? c(3, 1, -5) %in% 2 # Are elements in the vector equal to 2? ``` ### Example: Comparison ```{r ex-single-comparsion} x = c(51, 38, 42, -3) x == 38 x != 38 x < 38 x > 38 x <= 38 x >= 38 x %in% 38 ``` ### Exercise: Comparison ```{r ex-single-comparsion} x = c(1,2,3,4) x == 2 x != 2 x < 2 x > 2 x <= 2 x >= 2 x %in% 2 ``` ```{r ex-vectorization-comparsion} x = c(1,2,3,4) y = c(-1, 2, 6, 0) x == y x != y x < y x > y x <= y x >= y x %in% y ``` ```{r} x = c(0, 0.5, 1.0, 1.5, 2.0) y = 3*x - 2 y x x < 2 y y == 1 !(x < 2 | y == 1) ``` ## Example: Logical Subsets ```{r logic-subset} ex_vec = c(5, 3, -2, 42) ex_vec[c(TRUE, FALSE, TRUE, TRUE)] ``` ## Example: Filtering Observations ```{r logic-subset} ex_vec = c(5, 3, -2, 42) select_indices = ex_vec < 4 # Are values less than 4? ex_vec[select_indices] # Retrieve all values less than 4 ``` ## Exercise: Filter Weights Change the following piece of code to retrieve subjects whose weight is **more than** 240 ```{r weight-filter} weights = c(150, 220, 332, 250, 244, 185) ## Change this line from # weights[weights < 180] ``` ## Example: if-else bank account ```{r if-bank-acct} prob_opening_savings_acct = 0.91 # about 91% if( prob_opening_savings_acct > 0.80) { message("Target this user!") } else { message("Save our money and don't bother sending flyers.") } ``` ## Example: if-else ```{r example-if-else} x = -2L if(x < 0L) { -1 * x } else { x } ``` What happens if `x` is positive? negative? 0? ## Example: Classifying Determining `x` by comparing it against pre-set values. ```{r example-classify} x = "Jerry" if ( x != "Jerry" & x != "Elaine" & x != "George") { message("Soup for you!") } else { message("No soup for you!") } ``` An alternative way to write the above if. ```{r example-classify-in} if ( !(x %in% c("Jerry", "Elaine", "George")) ) { message("Soup for you!") } else { message("No soup for you!") } ``` ### Exercise: Determining parity of number Using the modulus operator, determine whether `x` is odd or even. Output using the `message()` function: - "x is even" - "x is odd" ```{r ex-if-number-parity} x = 1 # 1 modulus 2 1 %% 2 # 2 modulus 2 2 %% 2 3 %% 2 4 %% 2 ``` ```{r} x = 10 if(x %% 2 == 0) { message("x is even!") } else { message("x is odd!") } is_odd = function(x) { if(x %% 2 == 0) { message("x is even!") output = FALSE } else { message("x is odd!") output = TRUE } return(output) } is_odd(10) ``` ## Example: if-else-if-else Absolute Value ```{r} if ( x < 0 ) { -1 * x } else if( x == 0 ) { 0 } else { x } ``` ## Example: if-else-if-else Discriminant ```{r} a = 3; b = 2; c = -1 discriminant = b^2 - 4*a*c if ( discriminant > 0 ) { message("two real roots") } else if( discriminant == 0 ) { message("one real root") } else { message("two imaginary roots") } ``` ### Exercise: Grade Scale Write an if-else if-else statement for the grade scale. ```{r ex-grade-scale} grade_pct = 82 ``` ## Example: Classifying Temperature ```{r} temperature = c(92, 93, 81, 70, 68) temperature > 76 ifelse(temperature > 76, "hot", "cold") ``` ```{r} # Only one value is allowed in the conditional statement if(temperature > 76) { "hot" } else { "cold" } # Checking a vector inside of a _regular_ if # will be equivalent to looking at only the # first element. temperature[1] > 76 ``` ```{r} temperature > 76 # any - is checking for at least one TRUE # e.g. OR any(temperature > 76) # all - is checking for EVERYTHING being equal to TRUE # e.g. AND all(temperature > 76) all(temperature > 67) temperature > 67 ``` ## Example: Vectorized If-Else ```{r vectorized-bp} bp_data\$BP_Type = ifelse(bp_data\$Systolic < 120 , "Normal", ifelse( bp_data\$Systolic < 129, "Elevated", ifelse( bp_data\$Systolic < 139, "Stage 1", "Stage 2") ) ) ``` ## Exercise: Recoding "M" and "F" ```{r re-code-ifelse} x = c("M", "M", "F", "M", "F", "F") x ``` ## Example: Switch with recoding values ```{r} x = c("M", "M", "F", "M", "F", "F") x switch(x[1], "M" = "Male", "F" = "Female") ``` ## Exercise: Switch to determine OS ```{r os-switch} sys_name = Sys.info()[["sysname"]] ### Your code here... ``` ## Example: Vector Subsets ```{r examples-my-vec} # Create a vector my_vec = c(6.1, 5.5, 5.2, 5.9) # Retrieve _all_ data my_vec[] # First observation my_vec[1] # Remove the second observation: my_vec[-2] # Keep only the first two observations with integers: my_vec[c(1, 2)] # Keep only the first two observations with logicals: my_vec[c(TRUE, TRUE, FALSE, FALSE)] # Out of Bounds gives missingness my_vec[5] # Name our vectors names(my_vec) = c("Billy", "Angela", "Melissa", "Trevor") # Notice names appear my_vec # Extract Melissa's contents my_vec["Melissa"] ```