Replace the Elements of a Vector in R Programming – replace() Function
Last Updated :
01 Jun, 2020
replace()
function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values.
Syntax: replace(x, list, values)
Parameters:
x: vector
list: indices
values: replacement values
Example 1:
x < - c( "GFG" , "gfg" , "Geeks" )
x
y < - replace(x, 2 , "GeeksforGeeks" )
y
|
Output :
[1] "GFG" "gfg" "Geeks"
[1] "GFG" "GeeksforGeeks" "Geeks"
Example 2:
x < - c( "GFG" , "gfg" , "Geeks" )
x
y < - replace(x, c( 1 , 3 ), c( "A" , "B" ))
y
|
Output:
[1] "GFG" "gfg" "Geeks"
[1] "A" "gfg" "B"
Share your thoughts in the comments
Please Login to comment...