Reputation: 395
I have a data frame that looks as follows:
AA BB CC DD
aa 2.6 -0.3 2.5 2.3
bb -0.3 1.2 2.1 0.8
cc 0.5 -0.4 0.4 0.4
I would like to do a Quantile ranking for this data frame based on each column and mutate the ranking of each column to the Data frame. I would like to use dplyr package. I can do it for one column that results in following data frame:
AA BB CC DD quantile
aa 2.6 -0.3 2.5 2.3 4
bb -0.3 1.2 2.1 0.8 1
cc 0.5 -0.4 0.4 0.4 2
But I would like to mutate the quantile of all columns right after the corresponding column. Does anyone have any idea how can I do this in r with dplyr package with quantile and mutate functions?
Thanks!
Upvotes: 1
Views: 706
Reputation: 20095
You can try using dplyr::mutate_all
to apply quantile
and then cut
on all columns as:
library(dplyr)
df %>% mutate_all(funs(quant =
as.integer(cut(., quantile(.), include.lowest = TRUE))))
# AA BB CC DD AA_quant BB_quant CC_quant DD_quant
# 1 2.6 -0.3 2.5 2.3 4 2 4 4
# 2 -0.3 1.2 2.1 0.8 1 4 2 2
# 3 0.5 -0.4 0.4 0.4 2 1 1 1
Data:
df <- read.table(text="
AA BB CC DD
aa 2.6 -0.3 2.5 2.3
bb -0.3 1.2 2.1 0.8
cc 0.5 -0.4 0.4 0.4",
header = TRUE)
Upvotes: 4