Little useless-useful R functions – Script that generates calculator script

This time the simple useless function will generate a long list of IF clauses that will in fact return the result of a mathematical operation between two numbers.

This is not by no means any type of automatic generation of code, not a script, that will write itself.

The concept is fairly simple. Create a function that will return the results of a mathematical operation: addition, multiplication, division, subtraction, for two positive integers (whole number). Between 1 and 10. That’s it 🙂

# Basic concept
calc <- function(a,b,oper){
  if(a==1 & b==1 & oper=="+"){print("Result is 2")}
  if(a==1 & b==1 & oper=="-"){print("Result is 0")}
  if(a==1 & b==1 & oper=="*"){print("Result is 1")}
  if(a==1 & b==1 & oper=="/"){print("Result is 1")}
  }

calc(1,1,"-")
calc(1,1,"+")
calc(1,1,"*")
calc(1,1,"/")

I have played a little bit with combinations of 4 operations, and two times 10 numbers, yielding 400 combinations. And the function that generates the final calculate function is the following:

# set all combinations
df <- data.frame(merge(merge(c(1:10), c(1:10), by=NULL), c("+","-","/","*"), by=NULL))
colnames(df) <- c("numberA", "numberB", "oper")
f <- "calc <- function(a,b,oper){"
for (i in 1:nrow(df)){
  res <- paste0(as.character(df$numberA[i]) , df$oper[i], as.character(df$numberB[i]))
  rr <- eval(parse(text=res))
  f1 <- paste0(' if(a==',as.character(df$numberA[i]), ' & b==', as.character(df$numberB[i]), ' & oper==', '"',as.character(df$oper[i]),'"' ,
               '){print("Result is ', as.character(rr),'")}', '\n\r' , collapse=NULL)
  f <<- paste0(f, f1, collapse = NULL)
  if(i==nrow(df)){
    f <<- paste0(f, "}", collapse = NULL)    
    eval(parse(text=f))
    }
}

calc(4,5,"/")

Once the calc function for all 400 combinations is created, feel free to use it.

For some comparison, let’s crunch some numbers. I wanted to see how long does it take if the function get’s longer. The code for measuring the execution time:

start.time <- Sys.time()
calc(20,3,"+")
end.time <- Sys.time()
end.time - start.time

And the results for adding more positive integers is at least to say interesting, if not useless.

Number of integersNumber of combinationsFirst Execution time (sec)Average next execution time (sec)Size of function
104005,6s0.00122.3Mb
20160011,5s0.00229.3Mb
25250028,4s0.002714.5Mb
5010000717s0.008158Mb
10040000//232.1Mb

Hitting first 100 positive integers, the function increased to 230 Mb, taking space in memory. And based on the exponential growth of the size of the function, my laptop would hit “out of memory” boundary by the integer 1000. This makes you appreciate he usefulness of CPU architecture 🙂

As always, code is available on Github.

Happy R-Coding and stay healthy!

Tagged with: , , , ,
Posted in Uncategorized

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

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

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

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.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started