Learn R Programming

NMdata (version 0.2.3)

editCharCols: Replace strings in character character columns of a data set

Description

Replace strings in character character columns of a data set

Usage

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

Value

a data.frame

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.

Examples

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

Run the code above in your browser using DataLab