Little useless-useful R function

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 πŸ™‚

Tagged with: , ,
Posted in Uncategorized
8 comments on “Little useless-useful R function
  1. […] 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 […]

    Like

  2. RP says:

    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!

    Like

    • tomaztsql says:

      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.

      Like

  3. AmD says:

    Even shorter:

    randomCases <- function(inS){
    paste0(sapply(unlist(strsplit(inS, "")), function(x) ifelse(sample(0:1, 1) == 1, tolower(x),toupper(x))),collapse = "")
    }

    πŸ™‚

    AmD

    Liked by 1 person

  4. Alex Zolot says:

    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.")

    Like

  5. […] the first little useless-useful R function MixedCases gain a lot of interest in the R community, let us not stop here. […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories
Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Programs I use: Scraper API
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Ms SQL Girl

Julie Koesmarno's Journey In Data, BI and SQL World

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Server Insane Asylum (A Blog by Pat Wright)

Information about SQL Server from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

%d bloggers like this: