Start by loading the NHANES data and dplyr package.
library(dplyr)
library(NHANES)
data(NHANES)
In the previous practicum you categorized IQ scores in 2 categories with an if-statement and an ifelse function Section 3.3.2. We want to do the same with systolic blood pressure data (BPSysAve)from the NHANES dataset but this time using four categories instead of two.
| Blood Pressure Classification | systolic blood pressure |
|---|---|
| Normal | < 120 |
| Prehypertension | 120–139 |
| Stage 1 Hypertension | 140–159 |
| Stage 2 Hypertension | ≥ 160 |
Doing this with if-statements or ifelse functions would at least require three levels of nesting. This is where the tidyverse conditionals case_when and between come in handy.
Pitfall
All values that don’t meet a specified case will be replaced with NA by
case_when
BPCategory containing the classification name associated with the BPSysAve value for every row. Use the case_when and between conditionals. Overwrite the NHANES dataframe with your result.