Categorize dataframe by percentile in R

2019-09-09 18:02发布

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.

标签: r percentile
1条回答
爷的心禁止访问
2楼-- · 2019-09-09 18:27

(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
查看更多
登录 后发表回答