How to Recode Values in R

How to Recode Values in R, On sometimes, you might want to recode specific values in an R data frame. Fortunately, the recode() method from the dplyr package makes this simple to accomplish.

The use of this function is demonstrated using a number of examples in this lesson.

How to Use “not in” operator in Filter – Data Science Tutorials

Recoding a Single Column in a Dataframe as an Example

One column in a dataframe can be recoded using the code provided below.

library(dplyr)

Let’s create a data frame

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4'),
                 points = c(124, 229, 313, 415),
                 result = c('Win', 'Loss', 'Win', 'Loss'))

Now we can view the data frame

df
   player points result
1     P1    124    Win
2     P2    229   Loss
3     P3    313    Win
4     P4    415   Loss

Let’s change the ‘Win’ and ‘Loss’ to ‘1’ and ‘0’.

How to draw heatmap in r: Quick and Easy way – Data Science Tutorials

df %>% mutate(result=recode(result, 'Win'='1', 'Loss'='0'))
   player points result
1     P1    124      1
2     P2    229      0
3     P3    313      1
4     P4    415      0

Example 2: Provide NA values by recodifying a single column in a data frame.

A single column in a data frame can be recoded using the code below, which also assigns a value of NA to any values that are not expressly given a new value.

library(dplyr)

We can make use of the same dataframe again.

Best Books on Data Science with Python – Data Science Tutorials

df <- data.frame(player = c('P1', 'P2', 'P3', 'P4'),
                 points = c(124, 229, 313, 415),
                 result = c('Win', 'Loss', 'Win', 'Loss'))
df
   player points result
1     P1    124    Win
2     P2    229   Loss
3     P3    313    Win
4     P4    415   Loss

Now change the ‘Win’ to ‘1’ and give all other values a value of NA

df %>% mutate(result=recode(result, 'Win'='1', .default=NA_character_))
  player points result
1     P1    124      1
2     P2    229   <NA>
3     P3    313      1
4     P4    415   <NA>

Example 3: Multiple Columns in a Dataframe Can Be Recoded

The code that follows demonstrates how to simultaneously recode several columns in a dataframe.

How to perform One-Sample Wilcoxon Signed Rank Test in R? – Data Science Tutorials

Let’s recode ‘player’ and ‘result’ columns

df %>% mutate(player=recode(player, 'P1'='Q1'),
              result=recode(result, 'Win'='1', 'Loss'='0'))
  player points result
1     Q1    124      1
2     P2    229      0
3     P3    313      1
4     P4    415      0

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

seventeen − 13 =