Little useless-useful R functions – Finding total sum of all sub-arrays and sum of maximums

When it comes to O(N log(N)) time complexity functions, this is the right section. 🙂

Given an array of natural numbers (later, you will see, it can be real, or any other less complex system (rational, irrational)) and you create a tree of all permutations preserving conjugation order.

array = c(1,2,3,4)
#sub-array with sum in [] brackets
1. c(1) [1]
2. c(1,2)     [3]
3. c(1,2,3)  [6]
4. c(1,2,3,4) [10]
6. c(2) [2]
7. c(2,3) [5]
8. c(2,3,4) [9]
10. c(3) [3]
11. c(3,4) [7]
13. c(4) [4]

with total SUM = 1+3+6+10+2+5+9+3+7+4 = 50

With simple R function, we can achieve this by:

arr = c(1,2,3,4,4,5,6,7,7,6,5,4,3,1)

sumArr <- function(x){
  summ <- 0
  i <- 1
  for (i in 1:length(arr)) {
    j <- i + 0
    midsum <- 0
    for (j in j:length(arr)) {
      midsum <- sum(arr[i:j])
      summ <- summ + midsum
      #print(sum)
    }
  }
  cat(paste0("Total sum of sub-arrays: ", summ))
 }

#calling function
sumArr(arr)

Ok, this was useless and straightforward. What if we decide to find the maximums of each array and create a total sum:

array = c(1,2,3,4)
#sub-array with sum in [] brackets
1. c(1) [1]
2. c(1,2)     [2]
3. c(1,2,3)  [3]
4. c(1,2,3,4) [4]
6. c(2) [2]
7. c(2,3) [3]
8. c(2,3,4) [4]
10. c(3) [3]
11. c(3,4) [4]
13. c(4) [4]

with total SUM = 1+2+3+4+2+3+4+3+4+4 = 30
sumArrOfMax <- function(x){
  summ <- 0
  i <- 1
  for (i in 1:length(arr)) {
    j <- i + 0
    midsum <- 0
    for (j in j:length(arr)) {
      midsum <- max(arr[i:j])
      summ <- summ + midsum
      #print(sum)
    }
  }
  cat(paste0("Total sum of maximums of all sub-arrays: ", summ))
}

# run function
sumArrOfMax(arr)

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 Useless R functions

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