How to keep dropping the first value, until the sum of the vector is less than 20?
7
I am looking for a function which takes a vector and keeps dropping the first value until the sum of the vector is less than 20. Return the remaining values. I've tried both a for-loop and while-loop and can't find a solution. vec <- c(3,5,3,4,3,9,1,8,2,5) short <- function(vec){ for (i in 1:length(vec)){ while (!is.na((sum(vec)) < 20)){ vec <- vec[i+1:length(vec)] #vec.remove(i) } } The expected output should be: 1,8,2,5 which is less than 20.
r loops for-loop vector while-loop
share | improve this question
edited 5 hours ago
Khaynes
...