6 C
London
Tuesday, March 11, 2025
HomeStatistics TutorialRR: How to Replace Values in Data Frame Conditionally

R: How to Replace Values in Data Frame Conditionally

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 one of the following methods to replace values in a data frame conditionally:

Method 1: Replace Values in Entire Data Frame

#replace all values in data frame equal to 30 with 0
df[df == 30] 

Method 2: Replace Values in Specific Column

#replace values equal to 30 in 'col1' with 0
df$col1[df$col1 == 30] 

Method 3: Replace Values in Specific Column Based on Another Column

#replace values in col2 with 0 based on rows in col1 equal to 30
df$col2[df$col1 == 30] 

The following examples show how to use each method in practice with the following data frame:

#create data frame
df frame(team=c('A', 'A', 'B', 'B', 'B'),
                 points=c(99, 90, 90, 88, 88),
                 assists=c(33, 28, 31, 30, 34),
                 rebounds=c(30, 30, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    A     90      28       30
3    B     90      31       24
4    B     88      30       24
5    B     88      34       28

Method 1: Replace Values in Entire Data Frame

The following code shows how to replace all values equal to 30 in the data frame with 0:

#replace all values in data frame equal to 30 with 0
df[df == 30] #view updated data frame
df
  team points assists rebounds
1    A     99      33        0
2    A     90      28        0
3    B     90      31       24
4    B     88       0       24
5    B     88      34       28

Method 2: Replace Values in Specific Column

The following code shows how to replace all values equal to 90 in the ‘points’ column with 0:

#replace all values equal to 90 in 'points' column with 0
df$points[df$points == 90] #view updated data frame
df

  team points assists rebounds
1    A     99      33       30
2    A      0      28       30
3    B      0      31       24
4    B     88      30       24
5    B     88      34       28

Method 3: Replace Values in Specific Column Based on Another Column

The following code shows how to replace the values in the ‘points’ column with 0 where the value in the ‘team’ column is equal to ‘B.’

#replace all values equal to 90 in 'points' column with 0
df$points[df$team == 'B'] #view updated data frame
df

  team points assists rebounds
1    A     99      33       30
2    A     90      28       30
3    B      0      31       24
4    B      0      30       24
5    B      0      34       28

Additional Resources

The following tutorials explain how to perform other common operations in R:

R: How to Merge Data Frames Based on Multiple Columns
R: How to Add Column to Data Frame Based on Other Columns

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