简体   繁体   中英

Categorize dataframe by percentile in R

I have following data:

set.seed(15)
ddf <- data.frame(
    gp1 = sample(1:3, 200, replace=T), 
    gp2 = sample(c('a','b'), 200, replace=T), 
    param = sample(10:20, 200, replace=T) 
)
head(ddf)
  gp1 gp2 param
1   2   a    18
2   1   b    11
3   3   a    15
4   2   b    20
5   2   a    17
6   3   b    11

I have to create another column called 'category' which needs to have a value of 1 if 'param' for that row is more than 75th percentile for that gp1 and gp2.

I tried following but I am not sure if this is correct:

ddf$category = with(ddf, ifelse(param>quantile(ddf[ddf$gp1==gp1 & ddf$gp2==gp2,]$param, .75, na.rm=T), 1, 0)  )

Is above code correct or else how can this be done? Thanks for your help.

(After changing "value" to "param")

ddf = data.frame(gp1, gp2, param)
ddf$category <- with(ddf, ave(param, gp1,gp2, 
                             FUN=function(x) x > quantile(x,.95) ) )
> ddf
    gp1 gp2 param category
1     2   a    20        0
2     2   a    16        0
3     1   a    12        0
4     1   b    16        0
5     3   b    19        0
 snipped

> sum(ddf$category)
[1] 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM