Recoding allows you to create new variables and to replace existing values of a variables based on a criterion.
Replace the data for an existing variable
This way we can replace the data for every row without any criteria.
nhs_subset_1 is the new subsetted data frame which contains 100 rows and where the variable agegrp = 5 for all specified rows.
Replace data based on some criteria
In this case, we change the GROSRT to 5 only when it is 1000.
Replace data based on values in more than one variable
In the case above, we use multiple conditions to change the value for one variable, we set the GROSRT to 500 for males (SEX =2) with GROSRT equal to 1000.
We can also use the recode function to change the values of a group or range of variables.
To use the recode function, make sure you have the "car" package installed and loaded.
McGill Library • Questions? Ask us!Privacy notice