1

I am trying to implement a calculation that will rank the Price values in a separate partition. Below you can see my data

df<-data.frame( year=c(2010,2010,2010,2010,2010,2010),
                quarter=c("q1","q1","q1","q2","q2","q2"),
                Price=c(10,20,30,10,20,30)
                )

df

Now I want to count over each quarter and I expect to have 1 for the smallest Price and 3 for the highest Price

  df %>% group_by(quarter) %>% mutate(id = row_number(Price))

Instead of the expected results, I received different results. Below you can see the result from the code. Instead of ranking in separate quarter, ranging is in both quarters.

enter image description here

So can anybody help me how to solve this problem and to receive results as in table below

enter image description here

silent_hunter
  • 2,224
  • 1
  • 12
  • 30

3 Answers3

1

You probably want rank.

transform(df, id=ave(Price, year, quarter, FUN=rank))
#   year quarter Price id
# 1 2010      q1    10  1
# 2 2010      q1    20  2
# 3 2010      q1    30  3
# 4 2010      q2    10  1
# 5 2010      q2    20  2
# 6 2010      q2    30  3
jay.sf
  • 60,139
  • 8
  • 53
  • 110
1

With dplyr, use dense_rank

library(dplyr)
df %>% 
  group_by(quarter) %>% 
  mutate(id = dense_rank(Price)) %>% 
  ungroup
# A tibble: 6 × 4
   year quarter Price    id
  <dbl> <chr>   <dbl> <int>
1  2010 q1         10     1
2  2010 q1         20     2
3  2010 q1         30     3
4  2010 q2         10     1
5  2010 q2         20     2
6  2010 q2         30     3

In the newer version of dplyr, can also use .by in mutate

df %>%
  mutate(id = dense_rank(Price), .by = 'quarter')
   year quarter Price id
1 2010      q1    10  1
2 2010      q1    20  2
3 2010      q1    30  3
4 2010      q2    10  1
5 2010      q2    20  2
6 2010      q2    30  3
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Alternatively with row_number()

library(tidyverse)

df %>% group_by(year, quarter) %>% mutate(id=row_number())

Created on 2023-02-12 with reprex v2.0.2

# A tibble: 6 × 4
# Groups:   year, quarter [2]
   year quarter Price    id
  <dbl> <chr>   <dbl> <int>
1  2010 q1         10     1
2  2010 q1         20     2
3  2010 q1         30     3
4  2010 q2         10     1
5  2010 q2         20     2
6  2010 q2         30     3
jkatam
  • 2,691
  • 1
  • 4
  • 12