1
$\begingroup$

The following is a sample dataset, I'm only providing a snippet but there is more data than this. I need to know how compare each company is doing in terms of the different measures ( measure_1 and measure_2) based on the year and whether it fits in Prof_A or Prof_B. I'm using percentile rank: is that a good way to do this?

df=data.frame(company=c("A","B","C","D",
                        "E","A","B","C",
                        "G","H","J","K"),
            Year=c("2005","2005","2005","2005",
                     "2006","2006","2006","2006",
                     "2007","2007","2007","2007"),
              Prof_A=c("Y","Y","Y","N",
                       "Y","Y","N","N",
                       "Y","Y","Y","Y"),
              Prof_B=c("Y","N","N","N",
                       "Y","Y","Y","N",
                       "Y","N","Y","Y"),
              measure_1=c(1,5,90,2021,
                          45,646,65.66,90,
                          0.2,1,4.5,9), 
              measure_2=c(8,10,900,21,
                          5,66,5.66,23,
                          0.44,1.1,5.6,9.0))

a=df%>%group_by(Year)%>%
  mutate(Prof_A_PR_measure_1=ifelse(Prof_A=="Y",percent_rank(measure_1),NA),
         Prof_B_PR_measure_1=ifelse(Prof_B=="Y",percent_rank(measure_1),NA))
         
b= data.table::melt(data = a, id.vars = c("Year","company", "Prof_A","Prof_B","measure_1"), measure.vars = c("Prof_A_PR_measure_1", "Prof_B_PR_measure_1"),value.name="percent_rank")

Thanks!

$\endgroup$

1 Answer 1

0
$\begingroup$

I'm not 100% clear on the relationship between measures and profs (i.e. is Prof_A and Prof_B both being "Y" better than just one being "Y", or are they independent?).

Regardless, if I understand correctly, all you need is arrange()

a <- df %>%
  # gather measure columns
  gather(measure, val, contains("measure") %>%
  # desc() is used for Prof_A and Prof_B to get Y ahead of N
  arrange(Year, measure, desc(Prof_A), desc(Prof_B), val)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.