Useless_R_functions
Useless_R_functions copied to clipboard
Alternative approach to WordScrambler
Hi Tomaž. I know the typical idiom for scrambling letters in R involves strsplit.
Here's another approach: use charToRaw and rawToChar instead.
# Helper function. Can also be placed inside of WordScrambler() instead or be an
# anonymous function in the `tapply` step.
fun <- function(x) rawToChar(sample(x))
WordScrambler <- function(text) {
w <- charToRaw(text)
words <- cumsum(w == "20")
tolower(paste(tapply(w[w != "20"], words[w != "20"], fun), collapse = " "))
}