table(nhanes$hypertension)
#|
#| Elevated No Stage 1 Stage 2
#| 841 2657 1409 1216A Slower Introduction to R
UCSF Library
Thursday, November 20, 2025
factor() here:
nhanes$hypertension: the original variablec('No',...): the groups listed in the desired order| best for these data | possible values | groups can be ordered | |
|---|---|---|---|
| character | free text | many | no |
| factor | multiple choice | limited | yes |
At the top of a new script, include code for importing the data. Run your code.
table() the cholesterol variable (cholesterol). Which group is listed first? Check the object type. What is the object type?
factor() to re-order the groups in cholesterol so that they are in the following order: Desirable, Borderline high, and High. You can create a new variable (such as cholesterol_factor) or overwrite the existing cholesterol variable. Check your work. Verify that the groups are listed in the desired order in table().
labels argument:levels and labels must matchbase_match() function in baseverse:'label'=value formatbase_match() will honor the order of the groupsbase_match() orders the groups in the order specified in the second argumenttable():
base_match() and list the groups more than once:table():baseverse to the top of your script. Run your code.base_match() to create a labeled version of the original gender variable (riagendr). Name the new variable gender_base. Values of 1 represent males and values of 2 represent females. List the male group first. Check your work. Verify that the groups are listed in the desired order in table().base_match() to create a labeled version of race/ethnicity (ridreth3). You can call this variable race or race_ethnicity or whatever else makes sense to you. You may list the groups in whatever order you like. The NHANES documentation for ridreth3 is available here. Check your work. Verify that the groups are listed in the desired order in table().Add code for loading dplyr to the top of your script. Run your code.
Use case_match(), from dplyr, to create another labeled version of the original gender variable (riagendr). Name the new variable gender_dplyr. Again, list the male group first. case_match() works similarly to base_match(), except the value–label mappings should be listed in a value ~ 'label' format.
table() the gender_base variable. Check the class of the variable. What do you notice about the order of the groups and the object class?
table() the gender_dplyr variable. Check the class of the variable. What do you notice about the order of the groups and the object class?
lbxtc < 200 mg/dL → desirablelbxtc < 240 mg/dL → borderline highlbxtc ≥ 240 mg/dL → highbase_when():base_when() is a rule–label mapping in a label=rule formatbase_when() will honor the order of the groupswith():sum() and table():base_when() to define a variable for age group, with the following groups: 18–34, 35–54, and ≥ 55. The original age variable is ridageyr. Name the variable age3_base. Use the following labels: Youngest, Middle, and Oldest, and list the groups in that order. Check your work. Verify that the groups are listed in the desired order in table().NA values properly encoded. Now, use base_when() to define a categorical physical-activity variable with the following groups: less than 60 minutes, 60 minutes to less than 120 minutes, and 120 minutes or more. List the groups in order from fewer minutes to more minutes. Check your work. Verify that the groups are listed in the desired order in table().case_when(), from the dplyr, to create another variable for age group, with the following groups: 18–34, 35–54, and ≥ 55. The original age variable is ridageyr. Name the variable age3_dplyr. Use the following labels: Youngest, Middle, and Oldest, and list the groups in that order. case_when() works similarly to base_when(), except the rule–label mappings should be listed in a rule ~ 'label' format.table() the age3_base variable. Check the class of the variable. What do you notice about the order of the groups and the object class?
table() the age3_dplyr variable. Check the class of the variable. What do you notice about the order of the groups and the object class?
dplyr and the tidyverse way of doing thingsdplyr solutionsfor() loops, the apply functions, and other tools