Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • Control Chart in Quality Control
    Control Chart in Quality Control-Quick Guide Statistics
  • Applications of Data Science in Education
    Applications of Data Science in Education Machine Learning
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • ggpairs in R
    ggpairs in R R
How to Recode Values in R

How to Recode Values in R

Posted on June 24June 24 By Jim No Comments on How to Recode Values in R
Tweet
Share
Share
Pin

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

Check your inbox or spam folder to confirm your subscription.

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: Bind together two data frames by their rows or columns in R
Next Post: Tips for Rearranging Columns in R

Related Posts

  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • droplevels in R with examples
    droplevels in R with examples R
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • Best Books to Learn R Programming
    Best Books to Learn R Programming Course
  • How to handle Imbalanced Data
    How to handle Imbalanced Data? R
  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • OLS Regression in R
    OLS Regression in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme