The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
The following examples demonstrate using chars
objects
inside functions, no longer needing to spell out
strsplit(x, "")[[1]]
every time.
Which letters are vowels (aeiou)?
vowels <- function(word) {
ch <- chars(word)
setNames(ch %in% chars("aeiou"), ch)
}
vowels("string")
#> s t r i n g
#> FALSE FALSE FALSE TRUE FALSE FALSE
vowels("banana")
#> b a n a n a
#> FALSE TRUE FALSE TRUE FALSE TRUE
Since %in%
is vectorised, it works nicely with a
chars
object
“Strings” are finally iterable
Is a word a palindrome (spelled the same forwards and backwards)?
palindrome <- function(a, ignore_spaces = FALSE) {
a <- chars(a)
if (ignore_spaces) a <- except(a, " ")
all(rev(a) == a)
}
palindrome("palindrome")
#> [1] FALSE
palindrome("racecar")
#> [1] TRUE
palindrome("never odd or even", ignore_spaces = TRUE)
#> [1] TRUE
palindrome("go hang a salami im a lasagna hog", ignore_spaces = TRUE)
#> [1] TRUE
Are two words just rearrangements of their letters?
anagram <- function(a, b) {
is_anagram <- function(a, b) {
length(a) == length(b) && all(sort(a) == sort(b))
}
sapply(candidates, \(x) is_anagram(chars(a), chars(x)))
}
target <- "stressed"
candidates <- c("started", "desserts", "rested")
anagram(target, candidates)
#> started desserts rested
#> FALSE TRUE FALSE
Make every second letter uppercase
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.