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 29, 2018 in Data Analytics by DataKing99
• 8,250 points
815 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 29, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

+1 vote
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,720 points

edited Apr 3, 2018 by DeepCoder786 962 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,090 points
3,451 views
0 votes
1 answer

Test if a vector contains a given element

1. Making use of the std::count function Counting ...READ MORE

answered Jun 20, 2022 in Data Science by Sohail
• 3,040 points
519 views
0 votes
1 answer

Counting the number of elements with the values of x in a vector

You may simply use the table() method: > ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
638 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,049 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,660 points
1,226 views
+1 vote
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, 2019 in Data Analytics by anonymous
• 33,030 points
5,052 views
0 votes
1 answer

How to apply list to a function which give data frame as output

If you use  tidyverse, you can use ...READ MORE

answered Apr 11, 2018 in Data Analytics by Sahiti
• 6,370 points
850 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP