How to select only the last row among the subset of rows satisfying a condition in R programming

后端 未结 3 1176
被撕碎了的回忆
被撕碎了的回忆 2021-01-28 17:57

The dataframe looks like this :

Customer_id A B C D E F G
10000001    1 1 2 3 1 3 1
10000001    1 2 3 1 2 1 3
10000002    2 2 2 3 1 3 1
10000002    2 2 1 4 2 3 1         


        
3条回答
  •  旧时难觅i
    2021-01-28 18:38

    Assume your data is named dat,

    Here's one way using by and rbind, although the other two methods (aggregate and duplicated) are much nicer:

    > do.call(rbind, by(dat,dat$Customer_id,FUN=tail,1))
    ##    Customer_id A B C D E F G
    ## 2     10000001 1 2 3 1 2 1 3
    ## 4     10000002 2 2 1 4 2 3 1
    ## 7     10000003 1 1 2 2 1 2 1
    ## 11    10000004 1 4 1 4 1 3 1
    ## 13    10000006 1 3 1 4 1 2 1
    ## 16    10000008 1 3 1 1 2 2 1
    

提交回复
热议问题