find_replace.data.frame: Renames the values on a data.frame according to a...

Description Usage Arguments Examples

View source: R/find_replace.R

Description

Renames the values on a data.frame according to a pattern-value map. An index can be supplied to only operate on a selection of rows. An alternative loopup can be used which allows conditional replacement (e. g., to only change Mazda's in the mtcars dataset).

Usage

1
2
3
## S3 method for class 'data.frame'
find_replace(.DF, .index = NULL, .replace = NULL,
  .lookup = NULL, .name_vec, .all_variables = FALSE, ...)

Arguments

.DF

A dataframe.

.index

An optional index vector of the same length as .DF.

.replace

A list of variables changes are applied to.

.lookup

An optional variable that can be used as alternative lookup.

.name_vec

A vector that serves as a lookup map. This vector is of the form regexp = "replacement".

.all_variables

logic, if true, then all varibles will be selected. otherwise, only .replace will be selected

...

arguments passed on to other methods

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
model <- rownames(mtcars)
new= rep("Merc 1", dim(mtcars)[1])
mcars <- cbind(mtcars,
              model,
              new,
              stringsAsFactors=FALSE)

make <- c("(?i)^merc"="BMW")


find_replace(mcars,
             .replace="model",
             .name_vec=make)

find_replace(mcars,
            .replace=c("mpg" ,"model", "new"),
            .name_vec=make,
            .all_variables=FALSE)

find_replace(mcars,
            .name_vec=make,
            .all_variables = TRUE)

index <- with(mcars, model=="Fiat 128")
find_replace(mcars,
            .name_vec=make,
            .all_variables=TRUE,
            .index=index)

aotearoastats/datacleanr documentation built on May 6, 2019, 9:49 p.m.