Replace pattern everywhere in a data.frame

replace_pattern(dataset, pattern, replacement, exact = FALSE)

Arguments

dataset

a data.frame

pattern

Pattern to look for.

replacement

A character of replacements.

exact

a boolean if TRUE the whole value need ton match

Value

a data.frame

Examples

dataset <- data.frame(
  col_a = as.factor(letters)[1:7],
  col_b = letters[1:7],
  col_c = 1:7,
  col_d = paste0(letters[1:7], letters[1:7]),
  stringsAsFactors = FALSE
)

# replace pattern
replace_pattern(dataset, "a", "XXX-")
#>   col_a col_b col_c    col_d
#> 1  XXX-  XXX-     1 XXX-XXX-
#> 2     b     b     2       bb
#> 3     c     c     3       cc
#> 4     d     d     4       dd
#> 5     e     e     5       ee
#> 6     f     f     6       ff
#> 7     g     g     7       gg

# With exact matching
replace_pattern(dataset, "a", "XXX-", exact = TRUE)
#>   col_a col_b col_c col_d
#> 1  XXX-  XXX-     1    aa
#> 2     b     b     2    bb
#> 3     c     c     3    cc
#> 4     d     d     4    dd
#> 5     e     e     5    ee
#> 6     f     f     6    ff
#> 7     g     g     7    gg