Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • Changing the Font Size in Base R Plots
    Changing the Font Size in Base R Plots R
  • Data Science Applications in Banking
    Data Science Applications in Banking Machine Learning
  • test for normal distribution in r
    Test for Normal Distribution in R-Quick Guide R
  • Dynamic data visualizations in R
    Dynamic data visualizations in R R
  • Crosstab calculation in R
    Crosstab calculation in R R
  • Check whether any values of a logical vector are TRUE
    Check whether any values of a logical vector are TRUE 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

  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Calculate the p-Value from Z-Score in R
    Calculate the p-Value from Z-Score in R R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • Radar plot in R
    How to create Radar Plot in R-ggradar R
  • sorting in r
    Sorting in r: sort, order & rank R Functions 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
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • glm function in R
    glm function in r-Generalized Linear Models R
  • How to Label Outliers in Boxplots in ggplot2
    How to Label Outliers in Boxplots in ggplot2? R
  • Best Books on Data Science with Python
    Best Books on Data Science with Python Course
  • How to Create Summary Tables in R
    How to Create Summary Tables in R R
  • The Uniform Distribution in R
    The Uniform Distribution in R R
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • pheatmap function in R
    The pheatmap function in R R
  • How to Rank by Group in R?
    How to Rank by Group in R? R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme