There are times when the values in a variable are not the way you need them for your analyses. You might need to change the type data in a column (i.e,. change from character to numeric) or need to reorder or rearrange the values that you currently have in one or more variables.
This is generally referred to as recoding a variable. Recoding into a different variable transforms an original variable into a new variable. That is, the changes do not overwrite the original variable; they are instead applied to a copy of the original variable under a new name.
The following provides a couple of different ways to recode a variable.
There are a number of different ways to recode variables in R using Base R programming commands. Below review a couple of those ways to recode variables.
There are a number of ways to recode variables. These next four pages show you how the recode decisions are made and then shows three commands in Base R to recode a variable.
Look for "Live Demo" options to see how it works and looks in R Studio.
Rajesh Dorbala: Recoding Variables in R (9:52)
Richard Webster: Conditional Statements in R (9:29)
DataDaft: IF_Else statement in Base R Programming (2:33)
Recoding Values Recoding Values - dplyr package
Note: the dplry package is also loaded when you load the "tidyverse" package)
Duke Center for Comput Thinking: case_when() Functions with dplyr (4:10)
R Programming Tips: IF_Else statement in dplyr package (5:33)
Shows how to recode values in both the Base R and dplyr package
Sometimes you have to change a string variable into a numeric or convert a continuous variable into a factor. Here are some techniques to help you accomplish that objective
Statistics Globe: Convert Data Frame Column to Numeric in R (4:02)
Statistics Globe: Data Type Conversion in R (2 Examples)- 4:36
How to use gapminder and dplyr packages to convert data types