How to find percentile in r with many cateogories in a dataset? -
my dataset looks similar this:
category value1 value2 19 143 12 124 21 130 b 23 323 b 24 323 b 23 342 b 24 233 b 27 234 c 28 212 c 29 233 d 11 365 d 12 323 d 13 344
this dataset has many categories viz. a,b,c,d etc , 2 columns
how can find out 90th percentile of these values in category-wise?
output should in pattern:
try
library(dplyr) df1 %>% group_by(category) %>% summarise_each(funs(quantile(., 0.90))) # category value1 value2 #1 20.6 140.4 #2 b 25.8 334.4 #3 c 28.9 230.9 #4 d 12.8 360.8
or
library(data.table) setdt(df1)[, lapply(.sd, fun=quantile, prob=0.90), category]
or using aggregate
base r
aggregate(.~category, df1, fun=quantile, prob=0.90)
data
df1 <- structure(list(category = c("a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "d", "d", "d"), value1 = c(19, 12, 21, 23, 24, 23, 24, 27, 28, 29, 11, 12, 13), value2 = c(143, 124, 130, 323, 323, 342, 233, 234, 212, 233, 365, 323, 344)), .names = c("category", "value1", "value2"), row.names = c(na, -13l), class = "data.frame")
Comments
Post a Comment