How to Recode Values in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to Recode Values in R appeared first on Data Science Tutorials

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

The post How to Recode Values in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)