You can use the following syntax to replace a particular value in a data frame in R with a new value:
df[df == 'Old Value'] New value'
You can use the following syntax to replace one of several values in a data frame with a new value:
df[df == 'Old Value 1' | df == 'Old Value 2'] New value'
And you can use the following syntax to replace a particular value in a specific column of a data frame with a new value:
df['column1'][df['column1'] == 'Old Value'] New value'
The following examples show how to use this syntax in practice.
Example 1: Replace Particular Value Across Entire Data Frame
The following code shows how to replace one particular value with a new value across an entire data frame:
#create data frame df frame(a = as.factor(c(1, 5, 7, 8)), b = c('A', 'B', 'C', 'D'), c = c(14, 14, 19, 22), d = c(3, 7, 14, 11)) #view data frame df a b c d 1 1 A 14 3 2 5 B 14 7 3 7 C 19 14 4 8 D 22 11 #replace '14' with '24' across entire data frame df[df == 14] #view updated data frame df a b c d 1 1 A 24 3 2 5 B 24 7 3 7 C 19 24 4 8 D 22 11
Example 2: Replace One of Several Values Across Entire Data Frame
The following code shows how to replace one of several values with a new value across an entire data frame:
#create data frame df frame(a = as.factor(c(1, 5, 7, 8)), b = c('A', 'B', 'C', 'D'), c = c(14, 14, 19, 22), d = c(3, 7, 14, 11)) #view data frame df a b c d 1 1 A 14 3 2 5 B 14 7 3 7 C 19 14 4 8 D 22 11 #replace '14' and '19' with '24' across entire data frame df[df == 14 | df == 19] #view updated data frame df a b c d 1 1 A 24 3 2 5 B 24 7 3 7 C 24 24 4 8 D 22 11
Example 3: Replace Value in Specific Column of Data Frame
The following code shows how to replace one particular value with a new value in a specific column of a data frame:
#create data frame df frame(a = as.factor(c(1, 5, 7, 8)), b = c('A', 'B', 'C', 'D'), c = c(14, 14, 19, 22), d = c(3, 7, 14, 11)) #view data frame df a b c d 1 1 A 14 3 2 5 B 14 7 3 7 C 19 14 4 8 D 22 11 #replace '14' in column c with '24' df['c'][df['c'] == 14] #view updated data frame df a b c d 1 1 A 24 3 2 5 B 24 7 3 7 C 19 14 4 8 D 22 11
Example 4: Replace Values of a Factor Variable in Data Frame
If you attempt to replace a particular value of a factor variable, you will encounter the following warning message:
#create data frame df frame(a = as.factor(c(1, 5, 7, 8)), b = c('A', 'B', 'C', 'D'), c = c(14, 14, 19, 22), d = c(3, 7, 14, 11)) #attempt to replace '1' with '24' in column a df['a'][df['a'] == 1] A 14 3 2 5 B 14 7 3 7 C 19 14 4 8 D 22 11
To avoid this warning, you need to first convert the factor variable to a numeric variable:
#convert column a to numeric df$a numeric(as.character(df$a)) #replace '1' with '24' in column a df['a'][df['a'] == 1] #view updated data frame df a b c d 1 24 A 14 3 2 5 B 14 7 3 7 C 19 14 4 8 D 22 11
Additional Resources
How to Replace NAs with Strings in R
How to Impute Missing Values in R