How can values be replaced in a data frame in R?

How can values be replaced in a data frame in R?

Values in a data frame in R can be replaced by using the assignment operator, which is the “<-” symbol. This allows for the replacement of specific values or entire columns in a data frame. For example, to replace a specific value in a data frame named “df”, we can use the following syntax: “df[row, column] <- new_value”. This will replace the value at the specified row and column with the new value. To replace an entire column, we can use the same syntax, but leave the row index empty, as in “df[, column] <- new_column_values”. This will replace all values in the specified column with the new column values. Additionally, we can use conditional statements to replace values based on certain criteria. For example, “df[df$column == 0, column] <- new_value” will replace all values in the column that are equal to 0 with the new value. Overall, values in a data frame can be easily replaced in R using the assignment operator and various methods of indexing.

Replace Values in Data Frame in R (With Examples)


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 <- data.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] <- 24

#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 <- data.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] <- 24

#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 <- data.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] <- 24

#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

#create data frame
df <- data.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 adf['a'][df['a'] == 1] <- 24

Warning message:
In `[<-.factor`(`*tmp*`, thisvar, value = 24) :
  invalid factor level, NA generated
     a b  c  d
1 <NA> 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 <- as.numeric(as.character(df$a))

#replace '1' with '24' in column adf['a'][df['a'] == 1] <- 24

#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

Cite this article

stats writer (2024). How can values be replaced in a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-values-be-replaced-in-a-data-frame-in-r-can-you-provide-examples/

stats writer. "How can values be replaced in a data frame in R?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-values-be-replaced-in-a-data-frame-in-r-can-you-provide-examples/.

stats writer. "How can values be replaced in a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-values-be-replaced-in-a-data-frame-in-r-can-you-provide-examples/.

stats writer (2024) 'How can values be replaced in a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-values-be-replaced-in-a-data-frame-in-r-can-you-provide-examples/.

[1] stats writer, "How can values be replaced in a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can values be replaced in a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top