Recode in R – How To Reencode Data in R

When programming in data science, there are times when you need to reencode your data. There are many reasons for this from making your data more readable to maintaining security. For example, you may have numerical data that turns out to show interesting patterns when reencoded as letters. The reasons may vary, but the formula is the same.

Description

To recode in r you use the recode function which has the format of recode(input, factor) and it produces a reencoded version of the recode data based on the recode factor. Now “input” is the recode data of the function and “factor” is the recode factor. The recode factor tells the function how to reencode the input.
The input can be anything from a numeric variable to a character vector. You can even use multiple variables. You can add optional arguments such as factor levels the tell the recode function how to format the output. This function has a lot of flexibility that gives it a wide range of applications.

Explanation

The output of the recode function, is a categorical variable that only contains the values given to it by the recode factor. In this way, each value found in the variables is reencoded to the values assigned to them by the recode factor unless they are unmatched values or missing values.
The process is a form of translation, where you turn one set of values into another. Depending upon the sophistication that you put into this function, you could use it to do a lot of interesting things. The main idea is that you take the information into the recode function and output it in a different format and that format could be easier or harder to read.

Examples

Here are two examples of the recode function in action.

> x = rep(1:3,3)
> x
[1] 1 2 3 1 2 3 1 2 3
> recode(x, “c(1,2)=’A’; else=’B'”)
[1] “A” “A” “B” “A” “A” “B” “A” “A” “B”

In this example, you are reencoding the numeric values 1 and 2 into the character value “A” and a numeric value of 3 into the character value “B” making it non-reversible.

> x = rep(1:3,3)
> x
[1] 1 2 3 1 2 3 1 2 3
> recode(x, “1=’A’; 2=’B’; 3=’C'”)
[1] “A” “B” “C” “A” “B” “C” “A” “B” “X”

In this example, you are reencoding the numeric values 1, 2, and 3 to the character values “A”, “B”, and “C” respectively making it completely reversible.

Application

The most obvious application of the recode function is for encryption. It is quite possible to use this approach to encode characters into different characters or even numbers forming a type of code. It would be just as easy to reverse the process.
Creating categories would be another application of this function. You could set specific values to the category that you want it to be in. Then you could have two vectors one having your data and the other holding the categories.

If you are looking to reencode the data you are working with, the recode function is the way in r to do just that. Once you learn how to use it, this function is a highly flexible tool. It is a tool that you will find many ways of using.

Scroll to top
Privacy Policy