Replace strings in character character columns of a data set

editCharCols(data, pattern, replacement, as.fun, ...)

Arguments

data

The data set to edit.

pattern

Pattern to search for in character columns. Passed to `gsub()`. By default, `gsub()` works with regular expressions. See ... for how to disable this if you want to replace a specific string.

replacement

pattern or string to replace with. Passed to `gsub()`.

as.fun

The default is to return data as a data.frame. Pass a function (say tibble::as_tibble) in as.fun to convert to something else. If data.tables are wanted, use as.fun="data.table". The default can be configured using NMdataConf.

...

Additional arguments passed to `gsub()`. Especially, notice fixed=TRUE will disable interpretation of `pattern` and `replace` as regular expressions.

Value

a data.frame

Examples

### remove commas from character columns 
dat <- data.frame(A=1:3,text=cc(a,"a,d","g"))
editCharCols(dat,pattern=",","")
#>   A text
#> 1 1    a
#> 2 2   ad
#> 3 3    g
### factors are not edited but result in an error
if (FALSE) { # \dontrun{
dat <- data.frame(A=1:3,text=cc(a,"a,d",g),fac=cl("a","a,d","g"))
editCharCols(dat,pattern=",","")
} # }