Nothing spectacular, but yet interesting little useless R function for playing with strings and chars.
Converting Sentence case text to mixture of all and small caps resulting in sentence in mixed case.
For example:
"This is useless R Function that seems to exists."
to:
"This is UsEleSS r FuNcTion THaT SeEms To ExIsTS."
And many other mixture of cases. So by calling this function, the results will give you the mixed cases string.
MixedCases <- function(stavek){
nov_stavek = ""
cifra = 0
crka = ""
is.upper <- "[A-Z]"
is.lower <- "[a-z]"
for (crka in strsplit(stavek, "")[[1]]){
if (nchar(nov_stavek)<2){
random_stevilka = sample(0:1, 1, replace=TRUE)
if (random_stevilka == 0){
nov_stavek = paste(nov_stavek,toupper(crka), sep = "")
}
else {
nov_stavek = paste(nov_stavek,tolower(crka), sep = "")
}
}
else{
if(( grepl(pattern = is.upper, x=(strsplit(nov_stavek,"")[[1]][(cifra-2)])) &
grepl(pattern = is.upper, x=(strsplit(nov_stavek,"")[[1]][(cifra-1)])) |
grepl(pattern = is.lower, x=(strsplit(nov_stavek,"")[[1]][(cifra-2)])) &
grepl(pattern = is.lower, x=(strsplit(nov_stavek,"")[[1]][(cifra-1)])) ) == TRUE){
if ( grepl(pattern = is.upper, x=(strsplit(nov_stavek,"")[[1]][(cifra-1)])) ) {
nov_stavek = paste(nov_stavek, tolower(crka), sep = "")
}
else {
nov_stavek = paste(nov_stavek, toupper(crka), sep = "")
}
}
else {
random_stevilka = sample(0:1, 1, replace=TRUE)
if (random_stevilka == 0){
nov_stavek = paste(nov_stavek, toupper(crka), sep = "")
}
else {
nov_stavek = paste(nov_stavek, tolower(crka), sep = "")
}
}
}
#cifra = cifra + 1
}
return(nov_stavek)
}
Talk about useless functions π
Code is also available at Github. Feel free to improve it with even more wackiness π
Happy Rrrring π



[…] by data_admin [This article was first published on R β TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]
LikeLike
Hi Tomaz, fun useless function! I do think there’s room for making the code shorter by using lapply and removing some (what look like unnecessary) ifelse statements. My take on it:
random_cases <- function(string) {paste0(
lapply(1:nchar(string), function(char) { # Loop over characters in string
ifelse(sample(c(T, F), 1), # Toss a coin
toupper(substr(string, char, char)), # Convert to upper
tolower(substr(string, char, char))) # Convert to lower
}), collapse = "") # Stitch string back together
}
random_cases("This is an useless R Function that seems to exist")
I think it has similar behaviour, or am I missing something? Cheers!
LikeLike
Hi,
This is super awesome and much appreciated.
Had to make an extra stretch to write it in a useless manner. Lapply function is spot on function to do the job and I had a similar solution using lapply prepared, but decided to make it longer π
Thanks for your solution and support.
LikeLike
Even shorter:
randomCases <- function(inS){
paste0(sapply(unlist(strsplit(inS, "")), function(x) ifelse(sample(0:1, 1) == 1, tolower(x),toupper(x))),collapse = "")
}
π
AmD
LikeLiked by 1 person
Thank you AmD for your contribution. This one is Shortest yet π
LikeLike
a bit shortening AmD’s solution :
randomCases <- function(inS) paste0(sapply(strsplit(inS, "")[[1]],
function(x) ifelse(sample(0:1,1),toupper,tolower)(x)), collapse = "")
LikeLike
MixedCases <- function(stavek){
ss= strsplit(stavek, "")[[1]]
rn= sample(1:2, nchar(stavek), replace=TRUE)
s2= Map(function(s, i) list(toupper,tolower)[[i]](s), ss, rn)
return(paste0(s2, collapse=''))
}
MixedCases("This is useless R Function that seems to exists.")
LikeLike
[…] the first little useless-useful R function MixedCases gain a lot of interest in the R community, let us not stop here. […]
LikeLike
[…] one from the series [1,2,3,4] of useless functions. This one is particularly useful when you have overcast weather. And […]
LikeLike