e.g.
diamonds$carat %>% quantile(probs = seq(0.1:1, by = 0.05))
10% 15% 20% 25% 30% 35% 40% 45% 50% 55% 60% 65% 70% 75% 80% 85% 90% 95% 100%
0.31 0.32 0.35 0.40 0.42 0.50 0.53 0.60 0.70 0.73 0.90 1.00 1.01 1.04 1.13 1.24 1.51 1.70 5.01
Is there a clever 'tidyverse' way or otherwise to mutate a new column onto diamonds with the quantile from above that each observation belongs?
You can use cut.
data(diamonds, package="ggplot2")
library(dplyr)
diamonds %>%
mutate(tile5=cut(carat, breaks=quantile(carat, probs=seq(0,1,0.05)), include=T, labels=F)) %>%
select(carat, tile5)
# A tibble: 53,940 x 2
carat tile5
<dbl> <int>
1 0.23 1
2 0.21 1
3 0.23 1
4 0.290 1
5 0.31 2
6 0.24 1
7 0.24 1
8 0.26 1
9 0.22 1
10 0.23 1
# ... with 53,930 more rows
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With