Little useless-useful R functions – Letter frequency in a vector of numbers

So here is a useless fact. There is no letter A in numbers – written as words – from 1 to 100. And of course, we wanted to put this into the test and check if it holds the water.

And sure, there is the result:

Two sets of functions were created in this case (it can be stuffed in single one and super simplified, but it is all about uselessness.

Getting the words for number:

#function
word_a_number <- function(numb){

  basLet <- c('one','two','three','four','five','six','seven','eight','nine','ten'
              ,'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'
              ,'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety','one hundred')
  basNum <- c(1:20,30,40,50,60,70,80,90,100)
  df <- data.frame(num = basNum, let = as.character(basLet))
  
          if (numb <= 20) {
            im <- df[which(df$num == numb),]$let
            print(paste(im, collapse = NULL))
          } else {
                  if (numb %% 10 == 0){
                    e <- df[which(df$num == numb),]$let
                    print(paste0(e, collapse=NULL))
                  } else {
                    sec <- numb %% 10
                    fir <- as.integer(numb/10)*10
                    f_im <- df[which(df$num == fir),]$let
                    s_im <- df[which(df$num == sec),]$let
                    res <- paste0(f_im,"-",s_im, collapse = NULL)
                    print(res)
                  }
          }
}

This functions iterates through a set of numbers, given a boundaries. And since first ten words and and words from eleven to twenty differ significantly, I wrote it in data frame. Next step is to do the increments of ten. And then hundred, thousand, etc.

Once this function is established, it will return the word for given number. Once this is established, a little helper counter would do the job:

#function for  count the frequency 
freqLet <- function(x) {
  word <- tolower(unlist(strsplit(x,"")))
  word_table <- table(word)
  ans <- word_table[names(word_table)]
}

getFreq <- function(vect) {
  df <- data.frame(word=as.character(), stringsAsFactors = FALSE)
  for (i in 1:length(vect)) {
    df[i,1] <- as.character(word_a_number(i))
    a <<- freqLet(df$word)
  }
  return(a)
}

And once this is loaded into environment, we need to run the complete set:

######### Let's check the complete set of numbers

# Automate the function, get a vector of first 100 numbers
vect <- c(1:100)

#Is there A in first 100 words?
getFreq(vect)

And this returns a table of letter frequencies

There are some gotcha moments 🙂

It is useless (and that’s the catch) to write down the words of all the numbers. If you only create a dictionary of non-repetitive words (like the one from 11 to 19 or from 0 to 10), you may check only the in these subsets for presence of letter A 🙂

Another one is, if you – by some change – decide to write word as hundred and ten (110), you will get high frequency of letter A, unless you decide to write it as hundred-ten.

And the last gotcha moment is – letter A will be present only as in word AND as a helper word for easier pronunciation.

And really last gotcha – the next natural occurrence of letter A is in word gazillion 🙂 which is a loooot of billions and yet, very informal word.

As always, code is available in at the Github in same Useless_R_function repository.

Happy R-coding!

Tagged with: , , , , , , ,
Posted in Uncategorized, Useless R functions
3 comments on “Little useless-useful R functions – Letter frequency in a vector of numbers
  1. Anurag says:

    Hey Tomaz,
    It’s a great post.
    I guess you missed the A in thousand before gazillion. 🙂

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