This function lets the user replace all specific values in a vector or data.frame into another value. If replacing more than one value, order matters so they will be replaced in the same order that you pass them to the function. Factors will be refactored.
Arguments
- df
Data.frame or Vector
- original
String or Vector. Original text you wish to replace
- change
String or Vector. Values you wish to replace the originals with
- which
Character vector. Name of columns to use. Leave "all" for everything
- fixclass
Boolean. Try to detect logical classes after transformations (or leave as default classes as character)?
- quiet
Boolean. Keep quiet? (or print replacements)
See also
Other Data Wrangling:
balance_data()
,
categ_reducer()
,
cleanText()
,
date_cuts()
,
date_feats()
,
file_name()
,
formatHTML()
,
holidays()
,
impute()
,
left()
,
normalize()
,
num_abbr()
,
ohe_commas()
,
ohse()
,
quants()
,
removenacols()
,
replacefactor()
,
textFeats()
,
textTokenizer()
,
vector2text()
,
year_month()
,
zerovar()
Other Text Mining:
cleanText()
,
ngrams()
,
remove_stopwords()
,
sentimentBreakdown()
,
textCloud()
,
textFeats()
,
textTokenizer()
,
topics_rake()
Examples
df <- data.frame(
one = c(1:4, NA),
two = LETTERS[1:5],
three = rep("A", 5),
four = c(NA, "Aaa", 123, "B", "C")
)
print(df)
#> one two three four
#> 1 1 A A <NA>
#> 2 2 B A Aaa
#> 3 3 C A 123
#> 4 4 D A B
#> 5 NA E A C
replaceall(df, "A", NA)
#> # A tibble: 5 × 4
#> one two three four
#> <int> <chr> <lgl> <chr>
#> 1 1 NA NA NA
#> 2 2 B NA NA
#> 3 3 C NA 123
#> 4 4 D NA B
#> 5 NA E NA C
replaceall(df, "A", "a")
#> # A tibble: 5 × 4
#> one two three four
#> <int> <chr> <chr> <chr>
#> 1 1 a a NA
#> 2 2 B a aaa
#> 3 3 C a 123
#> 4 4 D a B
#> 5 NA E a C
replaceall(df, 1, "*")
#> # A tibble: 5 × 4
#> one two three four
#> <chr> <chr> <chr> <chr>
#> 1 * A A NA
#> 2 2 B A Aaa
#> 3 3 C A *23
#> 4 4 D A B
#> 5 NA E A C
replaceall(df, NA, "NotNA")
#> # A tibble: 5 × 4
#> one two three four
#> <chr> <chr> <chr> <chr>
#> 1 1 A A NotNA
#> 2 2 B A Aaa
#> 3 3 C A 123
#> 4 4 D A B
#> 5 NotNA E A C
replaceall(df, NA, 0)
#> # A tibble: 5 × 4
#> one two three four
#> <int> <chr> <chr> <chr>
#> 1 1 A A 0
#> 2 2 B A Aaa
#> 3 3 C A 123
#> 4 4 D A B
#> 5 0 E A C
replaceall(df, c("A", "B"), c("'A'", "'B'"))
#> # A tibble: 5 × 4
#> one two three four
#> <int> <chr> <chr> <chr>
#> 1 1 'A' 'A' NA
#> 2 2 'B' 'A' 'A'aa
#> 3 3 C 'A' 123
#> 4 4 D 'A' 'B'
#> 5 NA E 'A' C
replaceall(df, "a", "*", which = "four")
#> # A tibble: 5 × 4
#> one two three four
#> <int> <chr> <chr> <chr>
#> 1 1 A A NA
#> 2 2 B A A**
#> 3 3 C A 123
#> 4 4 D A B
#> 5 NA E A C