Little useless-useful R function
This article is originally published at https://tomaztsql.wordpress.com
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
Thanks for visiting r-craft.org
This article is originally published at https://tomaztsql.wordpress.com
Please visit source website for post related comments.