How to get the output of number of elements to reach a cumulative sum?

0 votes

I am trying to get an output consisting of the number of elements needed to be added to sum a certain value.
Below is a sample code considered.

  # value to reach
  vTR = c(10,15,12,13,10,15,10)
  # element to sum
  element = c(9,6,5,2,1,9,1)
  magicFoo(vTR, element) 
  # should return c(NA, 2, 3, 3, 4, 4, 2) 
  # 10 ~ NA, 15 <= 9+6, 12 <= 5+6+9, 13 <= 2+5+6, 10 <= 1+2+5+6...

For example, if I am looking for some kind of average where k is calculated dynamically I can do it with a for loop but I am finding a more elegant way to do this.

vTR = c(10,15,12,13,10,15,10)
# element to sum
element = c(9,6,5,2,1,9,1)
res = c()
j = 1
k = 0
sumE = 0
for (i in 1:length(vTR)){
  k = k+1
  sumE = sum(element[j:k])
  if (sumE < vTR[i]) {
    res[length(res)+1] = NA
    next
  }
  repeat {
    j = j + 1
    sumE = sum(element[j:k])
    if (sumE < vTR[i]) {
      j = j-1
      res[length(res)+1] = k-j +1 
      break
    }
  }
}
# > res
# [1] NA  2  3  3  4  4  2
May 28, 2018 in Data Analytics by DataKing99
• 8,130 points
40 views

1 answer to this question.

0 votes

You can use the sapply function, to loop over each element in vTR and take the first x values, then reverse them and take the cumulative sum over them and finally find the index when the value crosses the vTR[x] value.

Refer to the code below:

sapply(seq_along(vTR),function(x) which.max(cumsum(rev(head(element, x)))>=vTR[x]))

#[1] 1 2 3 3 4 4 2

To get the exact expected output we can modify it by

sapply(seq_along(vTR), function(x) {
  val = cumsum(rev(head(element, x)))
  if (sum(val)  >= vTR[x])
    which.max(val >= vTR[x])
  else
    NA
})

#[1] NA  2  3  3  4  4  2
answered May 28, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
2 answers
0 votes
1 answer

With the help of tidyverse: how to rename a column to a variable name

With the help of Dplyr: rename function ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,700 points

edited Apr 3, 2018 by DeepCoder786 41 views
0 votes
1 answer
0 votes
1 answer

How to limit output of a dataframe in R?

For randomly sampling a row/cell where a ...READ MORE

answered Apr 18, 2018 in Data Analytics by kappa3010
• 2,020 points
49 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
47 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
61 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
124 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
102 views
0 votes
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 25,620 points
94 views
0 votes
1 answer