- A+

Category：Languages

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.

You need to remove the first value each time, so your `while`

loop should be,

`while (sum(x, na.rm = TRUE) >= 20) { x <- x[-1] } #[1] 1 8 2 5 `