Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

replace_vars

Fast value replacement in data frame


Description

replace_vars could replace any value(s) or values that match specific patterns to another specific value in a data.table.

Usage

replace_vars(.data, ..., from = is.na, to)

Arguments

.data

A data.table

...

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 NaN.

to

A value.

Value

A data.table.

See Also

Examples

iris %>% as.data.table() %>%
   mutate(Species = as.character(Species))-> new_iris

 new_iris %>%
   replace_vars(Species, from = "setosa",to = "SS")
 new_iris %>%
   replace_vars(Species,from = c("setosa","virginica"),to = "sv")
 new_iris %>%
   replace_vars(Petal.Width, from = .2,to = 2)
 new_iris %>%
   replace_vars(from = .2,to = NA)
 new_iris %>%
   replace_vars(is.numeric, from = function(x) x > 3, to = 9999 )

tidyft

Tidy Verbs for Fast Data Operations by Reference

v0.4.5
MIT + file LICENSE
Authors
Tian-Yuan Huang [aut, cre] (<https://orcid.org/0000-0002-3591-4203>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.