Little useless-useful R functions – Word scrambler

This time the simple useless function will generate a scrambled word or will generated a sentence of scrambled words.

Scrambled word

Imagine the pangram – “The quick brown fox jumps over the lazy dog“. And that you want the letters in each word to be scrambled or shuffled.

So you have a text:

This is a successful writing of the Quick brown fox jumps over the lazy dog

And you get in return the following text:

sith is a ssclcufseu gntwtrii fo het uikcq brnow xfo spmuj over eht zayl dog

Little bit eyes boggling 🙂

The R function is fairly short:

#helper function
full_scramble <- function(s_word) {
  s_word <- as.character(s_word)
  i <- sample(1:nchar(s_word))
  sep_word <- unlist(strsplit(s_word, ""))
  paste(sep_word[i], collapse = "")
}


WordScrambler <- function(text){
  Words <- as.list(el(strsplit(Text, " ")))
  New_text <- paste0(unlist(sapply(1:length(Words), function(x) full_scramble(Words[x]))), collapse = " ")
  return(tolower(New_text))
}

And you can use the function as:

Text <- "This is a successful writting of the quick brown fox jumps over the lazy dog"
WordScrambler(Text)

Getting the results as:

As always, code is available on Github.

Happy R-Coding and stay healthy!

Tagged with: , , , , ,
Posted in Uncategorized, Useless R functions
7 comments on “Little useless-useful R functions – Word scrambler
  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. Ananda Mahto says:

    Here’s an alternative to consider:

    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 = " "))
    }

    Enjoy!

    Like

  3. AmD says:

    One liner just for fun

    paste(sapply(strsplit(Text,” “)[[1]], function (y) paste0(sapply(sample(nchar(y)),function(x) tolower(substring(y,x,x))),collapse = ”)),collapse = ” “)

    Like

  4. […] Little useless-useful R functions – Word scrambler […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
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 WordPress

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

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 Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...