Press "Enter" to skip to content

How to replace elements in R objects?

Zigya Acadmey 0

Replacing a value is very easy, thanks to replace() in R to replace the values.

In data analysis, there may be plenty of instances where you have to deal with missing values, negative values, or non-accurate values that are present in the dataset. These values might affect the analysis result as well.

So in order to avoid these situations and false accuracies, you can make use of replace() function in R to replace the false values with appropriate values.

Syntax

The replace() function in R syntax is very simple and easy to implement. It includes the vector, index vector, and the replacement values as well as shown below.

replace(x, list, values)

Arguments

  • x: vector having some values
  • list: this can be an index vector
  • values: the replacement values

Replace a value present in the vector

Let’s replace a value from a given vector

# Create a vector 
> fruits <- c('apple', 'lemon', 'grape', 'kiwi')
> fruits
[1] "apple"  "lemon"  "grape"  "kiwi"

# Let's replace 2nd element of the vector
> replace(fruits, 2, 'mango')
[1] "apple"  "mango"  "grape"  "banana"

# Let's replace 3rd and 4th element of vector
> replace(fruits, c(3:4), c('banana', 'pineapple'))
[1] "apple"     "lemon"     "banana"    "pineapple"

Replace the NA values with 0’s using replace() in R

So, we are going to replace the NA values with 0 which are present in the data frame. This is the input data frame having the NA values.

For this we are going to use airquality dataset because it got NA values in it so we can replace those NA values. Let’s check the airquality dataset.

# airquality dataset
> airquality
    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4      18     313 11.5   62     5   4
5      NA      NA 14.3   56     5   5
6      28      NA 14.9   66     5   6
7      23     299  8.6   65     5   7
8      19      99 13.8   59     5   8
9       8      19 20.1   61     5   9
10     NA     194  8.6   69     5  10
11      7      NA  6.9   74     5  11
...

Now, lets replace the NA values

> df <- airquality
> df <- replace(df, is.na(df), 0)
> df
    Ozone Solar.R Wind Temp Month Day
1      41     190  7.4   67     5   1
2      36     118  8.0   72     5   2
3      12     149 12.6   74     5   3
4      18     313 11.5   62     5   4
5       0       0 14.3   56     5   5
6      28       0 14.9   66     5   6
7      23     299  8.6   65     5   7
8      19      99 13.8   59     5   8
9       8      19 20.1   61     5   9
10      0     194  8.6   69     5  10
11      7       0  6.9   74     5  11
...

Replace logical values with o and 1 in a list

To replace logical values in a list we need to first create a list with logical values in it.

# Create a logical vector
> logical_vec <- c('TRUE', 'TRUE', 'FALSE', 'FALSE', 'TRUE')
> logical_vec
[1] "TRUE"  "TRUE"  "FALSE" "FALSE" "TRUE"

We can use if else to replace elements in a list

Syntax

ifelse( condition, v1, v2)

Arguments

  • condition: an expression that will return logical value either TRUE or FALSE.
  • v1: Executes when the condition is TRUE.
  • v2 Executes when the condition is FALSE.
> logical_vec <- ifelse(logical_vec=="TRUE", 0, 1)
> logical_vec
[1] 0 0 1 1 0

Conclusion

We completed replacing values in a data frame is a very handy option available in R for data analysis. Using replace() in R, you can switch NA, 0, and negative values with appropriate to clear up large datasets for analysis. Also saw how to use if-else in R.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published. Required fields are marked *