# replace

0th

Percentile

##### Replace Values in a Vector

replace replaces the values in x with indices given in list by those given in values. If necessary, the values in values are recycled.

Keywords
manip
##### Usage
replace(x, list, values)
##### Arguments
x

vector

list

an index vector

values

replacement values

##### Value

A vector with the values replaced.

##### Note

x is unchanged: remember to assign the result.

##### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

##### Aliases
• replace
Documentation reproduced from package base, version 3.6.2, License: Part of R 3.6.2

### Community examples

richie@datacamp.com at Jan 17, 2017 base v3.3.2

replace replaces values in an existing vector {r} replace( letters, c(1, 5, 9, 15, 21), c("A", "E", "I", "O", "U") )  You need to assign the result to a new variable – the original variable isn't overwritten {r} letters # same as it originally was  You can use other methods of indexing (negative integers, logical values and names) for the list argument. Replacement values are recycled. {r} replace(month.abb, -c(2, 4, 6, 9, 11), "31 days")  replace also works on lists {r} (l <- list( a = rbinom(20, 10, 0.5), b = "this element needs replacing" )) replace(l, "b", "that's better")  It even works on data frames. One use is to add columns (though with and within and dplyr::mutate are better at this). {r} trees replace(trees, 4, seq_len(nrow(trees)))