While replace_na_dt could replace all NAs to another value, replace_dt could replace any value(s) to another specific value.

replace_dt(.data, ..., from = is.nan, to = NA)

Arguments

.data

A data.frame

...

Colunms to be replaced. If not specified, use all columns.

from

A value, a vector of values or a function returns a logical value. Defaults to is.nan.

to

A value. Defaults to NA.

Value

A data.table.

See also

Examples

iris %>% mutate_vars(is.factor,as.character) -> new_iris

new_iris %>%
  replace_dt(Species, from = "setosa",to = "SS")
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <char>
#>   1:          5.1         3.5          1.4         0.2        SS
#>   2:          4.9         3.0          1.4         0.2        SS
#>   3:          4.7         3.2          1.3         0.2        SS
#>   4:          4.6         3.1          1.5         0.2        SS
#>   5:          5.0         3.6          1.4         0.2        SS
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica
new_iris %>%
  replace_dt(Species,from = c("setosa","virginica"),to = "sv")
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>             <num>       <num>        <num>       <num>  <char>
#>   1:          5.1         3.5          1.4         0.2      sv
#>   2:          4.9         3.0          1.4         0.2      sv
#>   3:          4.7         3.2          1.3         0.2      sv
#>   4:          4.6         3.1          1.5         0.2      sv
#>   5:          5.0         3.6          1.4         0.2      sv
#>  ---                                                          
#> 146:          6.7         3.0          5.2         2.3      sv
#> 147:          6.3         2.5          5.0         1.9      sv
#> 148:          6.5         3.0          5.2         2.0      sv
#> 149:          6.2         3.4          5.4         2.3      sv
#> 150:          5.9         3.0          5.1         1.8      sv
new_iris %>%
  replace_dt(Petal.Width, from = .2,to = 2)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <char>
#>   1:          5.1         3.5          1.4         2.0    setosa
#>   2:          4.9         3.0          1.4         2.0    setosa
#>   3:          4.7         3.2          1.3         2.0    setosa
#>   4:          4.6         3.1          1.5         2.0    setosa
#>   5:          5.0         3.6          1.4         2.0    setosa
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica
new_iris %>%
  replace_dt(from = .2,to = NA)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <char>
#>   1:          5.1         3.5          1.4          NA    setosa
#>   2:          4.9         3.0          1.4          NA    setosa
#>   3:          4.7         3.2          1.3          NA    setosa
#>   4:          4.6         3.1          1.5          NA    setosa
#>   5:          5.0         3.6          1.4          NA    setosa
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica
new_iris %>%
  replace_dt(is.numeric, from = function(x) x > 3, to = 9999 )
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>             <num>       <num>        <num>       <num>    <char>
#>   1:         9999      9999.0          1.4         0.2    setosa
#>   2:         9999         3.0          1.4         0.2    setosa
#>   3:         9999      9999.0          1.3         0.2    setosa
#>   4:         9999      9999.0          1.5         0.2    setosa
#>   5:         9999      9999.0          1.4         0.2    setosa
#>  ---                                                            
#> 146:         9999         3.0       9999.0         2.3 virginica
#> 147:         9999         2.5       9999.0         1.9 virginica
#> 148:         9999         3.0       9999.0         2.0 virginica
#> 149:         9999      9999.0       9999.0         2.3 virginica
#> 150:         9999         3.0       9999.0         1.8 virginica