How to assign value to data frame column when cumulative sum of another column exceeds certain value

A small query,
I have dataset like this

dataset <- data.frame("X"=sample(x = 100:1150,size = 100),"Y"=NA)

      X  Y
1    809 NA
2    521 NA  #NA changed to 1, because cumulative sum of X exceeded (809+521+607 > 1440)
3    607 NA  
4    163 NA  #NA changed to 1, because cumulative sum of X exceeded (521+607+163+179 > 1440)
5    179 NA  
6    647 NA #NA changed to 1, because cumulative sum of X exceeded (163+179+647+1086 > 1440)
7   1086 NA #NA changed to 1, because cumulative sum of X exceeded (1086 + 574 > 1440)
8    574 NA
9    390 NA #NA changed to 1, because cumulative sum of X exceeded (574 + 390 +864 > 1440)
10   864 NA #NA changed to 1, because cumulative sum of X exceeded (864 + 578 > 1440)
11   578 NA #NA changed to 1, because cumulative sum of X exceeded (578  +890 > 1440)
12   890 NA #NA changed to 1, because cumulative sum of X exceeded (890 + 1004 > 1440)
13  1004 NA #NA changed to 1, because cumulative sum of X exceeded (1004+906 > 1440)
14   906 NA
15   391 NA

how can I change Y to 1 when cumulative sum of X+1 term exceeds 1440.

Thank you for your time.

I think I can do this with loop,

j<-1

for(i in 1:nrow(dataset)){
  
  cumsum <- last(cumsum(dataset$X[j:i]))
  if(cumsum>=1440){dataset$Y[i-1]<-1
  
  cumsum<-0
  j<-i
  }
}

If, this can be done w/o loop it is highly appreciated.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.