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")