[R] subgroup-based quantiles

Rui Barradas ruipbarradas at sapo.pt
Fri Nov 30 19:07:54 CET 2012


Hello,

If you want Age quantiles by gender, you have to split the data by 
gender, apply the same code then recombine the result.


fun <- function(x){
     Age_group <- cut(x[, "Age"], labels=c(1:10),
         breaks=quantile(x[, "Age"], seq(0,1,.1)),
         include.lowest = TRUE)
     cbind(x, Age_group)
}

result <- do.call(rbind, lapply(split(dat, dat[, "GENDER"]), fun))
rownames(result) <- seq_len(nrow(result))
result


Hope this helps,

Rui Barradas
Em 30-11-2012 12:18, R Kozarski escreveu:
> Dear R users,
> given the patient sample with their Gender and Age
>          GENDER Age
>     [1,]      2  45
>     [2,]      1  58
>     [3,]      1  54
>     [4,]      2  71
>     [5,]      2  64
>   ...
> I would like to create an another column which groups the patients wrt
> Gender specific Age quantiles, following methodology similar to:
>
> Age_group <- cut(Age, labels=c(1:10), breaks=quantile(Age,
> seq(0,1,.1)),include.lowest = TRUE)
>
> The function above allows me to group only wrt Age quantiles.
>
> Best, Robert
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/subgroup-based-quantiles-tp4651415.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list