Sum of previous rows in a column R

前端 未结 1 566
傲寒
傲寒 2020-12-21 17:33

I have table as following

id State
1 True
2 False
3 True
4 False
5 False
6 True
7 True
8 False

I need to count true and false until showed

相关标签:
1条回答
  • 2020-12-21 17:37

    Does this do what you want?

    df$yes <- cumsum(df$State == "True")
    df$no <- cumsum(df$State == "False")
    

    Or if you have df$State as a logical vector

    df$yes <- cumsum(df$State)
    df$no <- cumsum(!df$State)
    
    0 讨论(0)
提交回复
热议问题