Running count based on field in R

后端 未结 3 1891
长情又很酷
长情又很酷 2021-02-15 13:33

I have a data set of this format

User       
1 
2
3
2
3
1  
1      

Now I want to add a column saying count which counts the occurrence of the

3条回答
  •  自闭症患者
    2021-02-15 14:17

    This is fairly easy with ave and seq.int:

    > ave(User,User, FUN= seq.int)
    [1] 1 1 1 2 2 2 3
    

    This is a common strategy and is often used when the items are adjacent to each other. The second argument is the grouping variable and in this case the first argument is really kind of a dummy argument since the only thing that it contributes is a length, and it is not a requirement for ave to have adjacent rows for the values determined within groupings.

提交回复
热议问题