Little useless-useful R functions – Plotting the decimal and binary conversion

How does the conversion between decimal to binary or from binary to decimal behave? With another useless function, I have plotted the points (x = decimal number, y = converted binary number) on a scatter plot. Just to find out that the graph shows the binomial distribution function. 🙂

First we create a useless function to convert from decimal to binary base:

dec2bin <- function(dec){
  dec_start <- dec
  str <- ''
  while (dec > 0) {
      if ((dec %% 2)==1){
        str <- paste0(str, '1')
      } else { #((dec %% 2)==0)
        str <- paste0(str, '0')
      }
    dec <- floor(dec/2)
  }
  splits <- strsplit(str, "")[[1]]
  reversed <- rev(splits)
  f_str <- paste(reversed, collapse = "")
  #return(paste("Decimal", dec_start, "is ",f_str," in binary"))
  return(f_str)
}

After we have the conversion function, we can create a dataset, that will hold the conversion results and the length of the binary number.

### Draw scatter plot for the conversion
df <- data.frame(dec_x = 1, bin_y = 1, digit_length=1)

for (i in 2:100){
    d <- c(dec_x = i, bin_Y = dec2bin(i), nchar(dec2bin(i)))
   df <-  rbind(df,d)
}

The data.frame can be created either way, from binary to decimal, respectively. The length of the binary number will be used to create groups of numbers for the scatterplot.

And finally, we create a scatter plot and line chart with smoothing line with the code:

library(ggplot2)
library(cowplot)
line <- ggplot(df, aes(x=dec_x, y=bin_y, colour=digit_length)) + geom_line()
line2 <- ggplot(df, aes(x=dec_x, y=bin_y)) + geom_line() + geom_smooth()
plot_grid(line, line2, labels = c("Length of binary digits", "Like a binominal distribution"))

And get final two graphs:

The left graph is the scatterplot with an x-axis of decimal numbers and the y-axis with binary numbers. The colour of the dots represents the length of the binary numbers. We can see that the steps appear with every circa every 20th number. This is when the length is increased and corresponding values are increased exponentially. The right graph shows the same behaviour of the conversion ( binary and decimal numbers) that clearly shows the similarities to the binomial distribution function. And the binomial distribution will appear regardless of the number of numbers converted.

As always, code is available on the Github in the same Useless_R_function repository. Check Github for future updates.

Happy R-coding and stay healthy!“

Tagged with: , , , ,
Posted in Uncategorized, Useless R functions
One comment on “Little useless-useful R functions – Plotting the decimal and binary conversion
  1. […] Tomaz Kastrun has run out of useless functions and has to create useful ones: […]

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