R - cumulative sum by condition

前端 未结 3 1513
感动是毒
感动是毒 2020-12-31 07:38

So I have a dataset which simplified looks something like this:

Year    ID     Sum
2009    999    100
2009    123     85
2009    666    100
2009    999    10         


        
3条回答
  •  没有蜡笔的小新
    2020-12-31 07:54

    Another way

    1) use ddply to sum a variable by group (similar to SQL group by)

    X <- ddply ( dataset, .(Year,ID), sum)
    

    2) merge the result with dataset

    Y <- merge( dataset, X, by=('Year','ID')
    

提交回复
热议问题