简体   繁体   中英

Creating a new ranking column based on conditions in R

I have a dataset that looks like this:

id  profile_id   company   product    price   
1      1           A        book      10.42
2      1           A        shirt     23.91
3      1           A        cup        5.95
4      2           B        book       7.99
5      2           B        shirt      5.95 
6      2           B        cup       11.76

I would like to create a new column, "rank", that shows the rank of the price per product, per company and per profile_id.

The output would look like this:

id  profile_id   company   product    price    rank 
1      1           A        book      10.42     2
2      1           A        shirt     23.91     3
3      1           A        cup        5.95     1
4      2           B        book       7.99     2
5      2           B        shirt      5.95     1
6      2           B        cup       11.76     3

I feel like this should be rather easy, but I can't really get this to work... Any help would be appreciated!

reproducible code:

df2 <- data.frame(id=c(1,2,3,4,5,6),
                  profile_id = c(1, 1, 1, 2, 2,2), 
                  company = c("A","A","A","B","B","B"), 
                  product = c("book", "shirt", "cup","book", "shirt", "cup"),
                  price = c(10.42, 23.91, 5.95, 7.99, 5.95, 11.76))

first group_by the "per company, profile_id" variables and then apply rank() :

library(dplyr)
df %>% group_by(company, profile_id) %>% mutate(rank = rank(price))

library(data.table)
df[,rank:=rank(price),by = .(company, profile_id)]

#     id profile_id company product price  rank
#1     1          1       A    book 10.42     2
#2     2          1       A   shirt 23.91     3
#3     3          1       A     cup  5.95     1
#4     4          2       B    book  7.99     2
#5     5          2       B   shirt  5.95     1
#6     6          2       B     cup 11.76     3

我们可以使用base R来做到这一点

df$rank <- with(df, ave(price, company, profile_id, FUN = rank))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM