Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use dplyr::percent_rank() to compute percentile ranks within group

Tags:

r

dplyr

Suppose I have the following data:

id    grpvar1    grpvar2    value
1     1          3          7.6
2     1          2          4
...
3     1          5          2

For each id, I want to compute the percent_rank() of its value within the group defined by the combination of grpvar1 and grpvar2.

Using data.table, I would go (assuming I my data is in a data.frame called dataf:

library(data.table)

# Make dataset into a data.table.
dt <- data.table(dataf)

# Calculate the percentiles.
dt[, percrank := rank(value)/length(value), by = c("grpvar1", "grpvar2")]

What is the equivalent in dplyr?

like image 884
Brash Equilibrium Avatar asked Sep 10 '14 08:09

Brash Equilibrium


Video Answer


1 Answers

Try:

 library(dplyr)
 dataf %>%
 group_by(grpvar1, grpvar2) %>% 
 mutate(percrank=rank(value)/length(value))
like image 131
akrun Avatar answered Sep 16 '22 18:09

akrun