How to Apply the replace() Function in R (Example Code)

On this page you’ll learn how to exchange values in a vector using the replace function in R.

Creating Exemplifying Data

my_vec <- letters[1:5]             # Creating vector
my_vec                             # Returning example vector
# "a" "b" "c" "d" "e"

Example: Using replace() Function to Replace Certain Values in Vector

replace(my_vec, c(1, 3), "XXX")    # Using replace function
# "XXX" "b"   "XXX" "d"   "e"

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed