Skip to content
Back to formatted view

Raw Message

Message-ID: <1354301441.51214.YahooMailNeo@web142605.mail.bf1.yahoo.com>
Date: 2012-11-30T18:50:41Z
From: arun
Subject: subgroup-based quantiles
In-Reply-To: <50B8F5FA.30802@sapo.pt>

Hi,
You could also try ?ave()
dat$Age_group<-ave(dat$Age,dat$GENDER,FUN=function(x){cut(x,labels=1:10,breaks=quantile(x,seq(0,1,.1)),include.lowest=TRUE)})
?dat
#? GENDER Age Age_group
#1????? 2? 45???????? 1
#2????? 1? 58??????? 10
#3????? 1? 54???????? 1
#4????? 2? 71??????? 10
#5????? 2? 64???????? 5
A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: R Kozarski <r.kozarski at gmail.com>
Cc: r-help at r-project.org
Sent: Friday, November 30, 2012 1:07 PM
Subject: Re: [R] subgroup-based quantiles

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.

______________________________________________
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.