replace {mStats} | R Documentation |
Change contents of an existing variable
Description
replace()
alters the contents of a variable when specified
conditions are met.
Usage
replace(data, var, value, ...)
Arguments
data |
data.frame |
var |
variable |
value |
value for replacement |
... |
|
Details
If only value
is specified, the whole variable is assigned
with the value
. Multiple conditions can be specified within
the three dots.
Value
data.frame
Author(s)
Email: dr.myominnoo@gmail.com
Website: https://myominnoo.github.io/
Examples
x <- replace(infert, case, 2, case == 0)
tab(x, case)
x <- replace(infert, parity, 4, parity > 4)
tab(x, parity)
## Not run:
## More examples
## replacing mpg with standardized values of mpg
replace(mtcars, mpg, mpg / mean(mpg))
## replacing mpg with NA if < 10 or > 20
replace(mtcars, mpg, NA, mpg < 10 | mpg > 20)
## replacing education levels with one value
replace(infert, education, "6+yrs",
education == "6-11yrs" | education == "12+ yrs")
## replacing mpg with NA if mpg is from 10 and 20.
replace(mtcars, mpg, NA, mpg >= 10, mpg <= 20)
## End(Not run)
[Package mStats version 3.4.0 Index]