11.1 C
London
Sunday, July 7, 2024
HomeTidyverse in Rdplyr in RHow to Replace Multiple Values in Data Frame Using dplyr

How to Replace Multiple Values in Data Frame Using dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to replace multiple values in a data frame in R using functions from the dplyr package:

library(dplyr)

df %>%
  mutate(var1 = recode(var1, 'oldvalue1' = 'newvalue1', 'oldvalue2' = 'newvalue2'), 
         var2 = recode(var2, 'oldvalue1' = 'newvalue1', 'oldvalue2' = 'newvalue2'))

The following example shows how to use this syntax in practice.

Example: Replace Multiple Values Using dplyr

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df frame(conf=c('East', 'East', 'West', 'West', 'North'),
                 position=c('Guard', 'Guard', 'Guard', 'Guard', 'Forward'),
                 points=c(22, 25, 29, 13, 18))

#view data frame
df

   conf position points
1  East    Guard     22
2  East    Guard     25
3  West    Guard     29
4  West    Guard     13
5 North  Forward     18

Now suppose we would like to replace the following values in the data frame:

  • ‘conf’ column:
    • Replace ‘East’ with ‘E’
    • Replace ‘West’ with ‘W’
    • Replace ‘North’ with ‘N’
  • ‘position’ column:
    • Replace ‘Guard’ with ‘G’
    • Replace ‘Forward’ with ‘F’

We can use the mutate() and recode() functions to do so:

library(dplyr) 

#replace multiple values in conf and position columns
df %>%
  mutate(conf = recode(conf, 'East' = 'E', 'West' = 'W', 'North' = 'N'), 
         position = recode(position, 'Guard' = 'G', 'Forward' = 'F'))

  conf position points
1    E        G     22
2    E        G     25
3    W        G     29
4    W        G     13
5    N        F     18

Notice that each of the values in the ‘conf’ and ‘position’ columns have been replaced with specific values.

Also notice that the values in the ‘points’ column have remain unchanged.

Additional Resources

The following tutorials explain how to perform other common tasks using dplyr:

How to Recode Values Using dplyr
How to Replace NA with Zero in dplyr
How to Filter Rows that Contain a Certain String Using dplyr

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories