Skip to content

Data Science Tutorials

For Data Science Learners

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Contact
  • About Us
  • Toggle search form
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • Solving Systems of Equations in R R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Psychological Experimentation Software
    Psychological Experimentation Software: OpenSesame Opensesame
  • How to perform kruskal wallis test in r
    How to perform the Kruskal-Wallis test in R? R
  • How to deal with text in R
    How to deal with text in R R
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Calculate the P-Value from Chi-Square Statistic in R
    Calculate the P-Value from Chi-Square Statistic in R R
How to Recode Values in R

How to Recode Values in R

Posted on June 24June 24 By Admin No Comments on 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

Check your inbox or spam folder to confirm your subscription.

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

  • how to create a hexbins chart in R
    How to create a hexbin chart in R R
  • How to handle Imbalanced Data
    How to handle Imbalanced Data? R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • How to add Axes to Plot in R R
  • Number to Percentage in R
    Number to Percentage in R R
  • How to Count Distinct Values in R
    How to Count Distinct Values in R R

Leave a Reply Cancel reply

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

  • Multiple regression model in R
  • Descriptive statistics in R
  • How to Find the Size of a Data Frame in R
  • Filter a Vector in R
  • Split a Vector into Chunks in R
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • How do confidence intervals work
    How do confidence intervals work? R
  • Error in solve.default(mat)  Lapack routine dgesv system is exactly singular
    Error in solve.default(mat) :  Lapack routine dgesv: system is exactly singular: U[2,2] = 0 R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
  • How to Prepare a Machine Learning Interview
    How to Prepare a Machine Learning Interview? Machine Learning
  • Artificial Intelligence Examples
    Artificial Intelligence Examples-Quick View Course
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Data Science Strategies for Improving Customer Experience in R
    Data Science Strategies for Improving Customer Experience in R R

Privacy Policy

Copyright © 2024 Data Science Tutorials.

Powered by PressBook News WordPress theme