suppressPackageStartupMessages(library("tidyverse"))
package 㤼㸱tidyverse㤼㸲 was built under R version 3.6.3
str_replace_all("past/present/future", "/", "\\\\")
[1] "past\\present\\future"
str_to_lower()
using replace_all()
.replacements <- c(
"A" = "a", "B" = "b", "C" = "c", "D" = "d", "E" = "e",
"F" = "f", "G" = "g", "H" = "h", "I" = "i", "J" = "j",
"K" = "k", "L" = "l", "M" = "m", "N" = "n", "O" = "o",
"P" = "p", "Q" = "q", "R" = "r", "S" = "s", "T" = "t",
"U" = "u", "V" = "v", "W" = "w", "X" = "x", "Y" = "y",
"Z" = "z"
)
lower_words <- str_replace_all(words, pattern = replacements)
head(lower_words)
[1] "a" "able" "about" "absolute" "accept" "account"
First, make a vector of all the words with first and last letters swapped,
swapped <- str_replace_all(words, "^([A-Za-z])(.*)([a-z])$", "\\3\\2\\1")
Next, find what of “swapped” is also in the original list using the function intersect()
,
intersect(swapped, words)
[1] "a" "america" "area" "dad" "dead" "lead" "read"
[8] "depend" "god" "educate" "else" "encourage" "engine" "europe"
[15] "evidence" "example" "excuse" "exercise" "expense" "experience" "eye"
[22] "dog" "health" "high" "knock" "deal" "level" "local"
[29] "nation" "on" "non" "no" "rather" "dear" "refer"
[36] "remember" "serious" "stairs" "test" "tonight" "transport" "treat"
[43] "trust" "window" "yesterday"