R: How to replace values in data frame conditionally?

Replacing values in a data frame conditionally is the process of allowing a user to selectively replace values in a data frame with different values based on specific conditions that the user defines. This can be done by using the ifelse statement, which will check for a specified condition and then execute the code for either the “true” or “false” result. Using this statement, a data frame can be manipulated to replace values that meet a certain condition with a new value.


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] <- 0

Method 2: Replace Values in Specific Column

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

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] <- 0 

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

#create data frame
df <- data.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] <- 0

#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] <- 0

#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'] <- 0

#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

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

x