R data frame rank by groups (group by rank) with package dplyr

后端 未结 3 874
别跟我提以往
别跟我提以往 2021-01-03 15:46

I have a data frame \'test\' that look like this:

    session_id  seller_feedback_score
1   1   282470
2   1   275258
3   1   275258
4   1   275258
5   1   3         


        
相关标签:
3条回答
  • 2021-01-03 16:01

    One way you can do this is :

    dataset<-dataset%>%arrange(ID, DateTime,Index)
    dataset$Rank<-c(0,ID)[-(nrow(dataset)+1)] == ID
    dataset<- dataset%>%group_by(ID)%>%mutate(Rank = cumsum(Rank))
    

    Had the same issue!

    0 讨论(0)
  • 2021-01-03 16:03

    Had a similar issue, my answer was sorting on groups and the relevant ranked variable(s) in order to then use row_number() when using group_by.

    # Sample dataset
    df <- data.frame(group=rep(c("GROUP 1", "GROUP 2"),10),
                   value=as.integer(rnorm(20, mean=1000, sd=500)))
    require(dplyr)
    print.data.frame(df[0:10,])
       group value
    1  GROUP 1  1273
    2  GROUP 2  1261
    3  GROUP 1  1189
    4  GROUP 2  1390
    5  GROUP 1  1942
    6  GROUP 2  1111
    7  GROUP 1   530
    8  GROUP 2   893
    9  GROUP 1   997
    10 GROUP 2   237
    
    sorted <- df %>% 
              arrange(group, -value) %>%
              group_by(group) %>%
              mutate(rank=row_number())
    print.data.frame(sorted)
    
          group value rank
    1  GROUP 1  1942    1
    2  GROUP 1  1368    2
    3  GROUP 1  1273    3
    4  GROUP 1  1249    4
    5  GROUP 1  1189    5
    6  GROUP 1   997    6
    7  GROUP 1   562    7
    8  GROUP 1   535    8
    9  GROUP 1   530    9
    10 GROUP 1     1   10
    11 GROUP 2  1472    1
    12 GROUP 2  1390    2
    13 GROUP 2  1281    3
    14 GROUP 2  1261    4
    15 GROUP 2  1111    5
    16 GROUP 2   893    6
    17 GROUP 2   774    7
    18 GROUP 2   669    8
    19 GROUP 2   631    9
    20 GROUP 2   237   10
    
    0 讨论(0)
  • 2021-01-03 16:12

    Found an answer in : Add a "rank" column to a data frame

    data.selected <- transform(data.selected, 
                  seller_feedback_score_rank = ave(seller_feedback_score, session_id, 
                                  FUN = function(x) rank(-x, ties.method = "first")))
    
    0 讨论(0)
提交回复
热议问题