Hi,everyone i need to calculate quartile values of a variable grouped by the other variable . same as in aggregate function(only median,mean or functions is possible-i think so) Could you please help me to achieve the same for other quartile values(5,10,25,75,90) as for median using aggregate. Thanks in advance. data : zip price 60000 567000 60001 478654 60004 485647 60001 2783958 60005 97845848 60006 378383478 60002 397895735 60001 487587575 60002 478848 60003 49847874 60004 467648 60005 567489 60006 4776746 60004 4843949 what i want i quartile values of price grouped by zip -- View this message in context: http://www.nabble.com/use-of-class-variable-in-r-as-in-Proc-means-of-sas-tp25530654p25530654.html Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2009-Sep-22 13:21 UTC
[R] Odp: use of class variable in r as in Proc means of sas
Hi r-help-bounces at r-project.org napsal dne 22.09.2009 11:51:18:> > Hi,everyone i need to calculate quartile values of a variable grouped bythe> other variable . > same as in aggregate function(only median,mean or functions ispossible-i> think so) > Could you please help me to achieve the same for other quartile > values(5,10,25,75,90) as for median using aggregate. > Thanks in advance. > data : > zip price > 60000 567000 > 60001 478654 > 60004 485647 > 60001 2783958 > 60005 97845848 > 60006 378383478 > 60002 397895735 > 60001 487587575 > 60002 478848 > 60003 49847874 > 60004 467648 > 60005 567489 > 60006 4776746 > 60004 4843949 > what i want i quartile values of price grouped by zipIf you need several values of quantiles together aggregate is not suitable as the function needs to return scalar aggregate(rnorm(60), list(sample(1:5,60, replace=T)), function(x) quantile(x,c(.05,.5,.95))) Error in aggregate.data.frame(as.data.frame(x), ...) : 'FUN' must always return a scalar but you can use by by(rnorm(60), list(sample(1:5,60, replace=T)), function(x) quantile(x,c(.05,.5,.95))) or some other aggregating functions e.g. in doBy or plyr packages or to use split/sapply construct. Regards Petr> -- > View this message in context:http://www.nabble.com/use-of-class-variable-in-> r-as-in-Proc-means-of-sas-tp25530654p25530654.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Is this what you want:> xzip price 1 60000 567000 2 60001 478654 3 60004 485647 4 60001 2783958 5 60005 97845848 6 60006 378383478 7 60002 397895735 8 60001 487587575 9 60002 478848 10 60003 49847874 11 60004 467648 12 60005 567489 13 60006 4776746 14 60004 4843949> tapply(x$price, x$zip, quantile, prob=c(0,.05,.1,.75,.9))$`60000` 0% 5% 10% 75% 90% 567000 567000 567000 567000 567000 $`60001` 0% 5% 10% 75% 90% 478654.0 709184.4 939714.8 245185766.5 390626851.6 $`60002` 0% 5% 10% 75% 90% 478848 20349692 40220537 298541513 358154046 $`60003` 0% 5% 10% 75% 90% 49847874 49847874 49847874 49847874 49847874 $`60004` 0% 5% 10% 75% 90% 467648.0 469447.9 471247.8 2664798.0 3972288.6 $`60005` 0% 5% 10% 75% 90% 567489 5431407 10295325 73526258 88118012 $`60006` 0% 5% 10% 75% 90% 4776746 23457083 42137419 284981795 341022805 On Tue, Sep 22, 2009 at 5:51 AM, premmad <mtechprem at gmail.com> wrote:> > Hi,everyone i need to calculate quartile values of a variable grouped by the > other variable . > same as in aggregate function(only median,mean or functions is possible-i > think so) > Could you please help me to achieve the ?same for other quartile > values(5,10,25,75,90) as for median using aggregate. > Thanks in advance. > data : > zip ? ? ? ? ? ? price > 60000 ? 567000 > 60001 ? 478654 > 60004 ? ? ?485647 > 60001 ? ? ?2783958 > 60005 ? ? ?97845848 > 60006 ? ? ?378383478 > 60002 ? ? ?397895735 > 60001 ? ? ?487587575 > 60002 ? ? ?478848 > 60003 ? ? ?49847874 > 60004 ? ? ?467648 > 60005 ? ? ? 567489 > 60006 ? ? ? 4776746 > 60004 ? ? ? 4843949 > what i want i quartile values of price grouped by zip > -- > View this message in context: http://www.nabble.com/use-of-class-variable-in-r-as-in-Proc-means-of-sas-tp25530654p25530654.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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Here's the code that does the job for quartiles (0,25,50,75,100). To get to your objective of (5,10,25,75,90) is left as an exercise. There are several well-written introductory books in R, in addition to the freely available presentations and other online resources. I think you should spend some time going thru' them. ---------------------- library(doBy) Lines <- "zip price 60000 567000 60001 478654 60004 485647 60001 2783958 60005 97845848 60006 378383478 60002 397895735 60001 487587575 60002 478848 60003 49847874 60004 467648 60005 567489 60006 4776746 60004 4843949" DF <- read.table(con<- textConnection(Lines), skip = 1) close(con) names(DF) <- scan(textConnection(Lines), what = "", nlines = 1) qfun <- function(x, digits=3,sci=F,...){ c(q=quantile(x, ...)) } summaryBy(price~zip,data=DF,FUN=qfun,na.rm=TRUE) -------------------------------------- cheers, -Girish ========================== premmad wrote:> > Hi,everyone i need to calculate quartile values of a variable grouped by > the other variable . > same as in aggregate function(only median,mean or functions is possible-i > think so) > Could you please help me to achieve the same for other quartile > values(5,10,25,75,90) as for median using aggregate. > Thanks in advance. > data : > zip price > 60000 567000 > 60001 478654 > 60004 485647 > 60001 2783958 > 60005 97845848 > 60006 378383478 > 60002 397895735 > 60001 487587575 > 60002 478848 > 60003 49847874 > 60004 467648 > 60005 567489 > 60006 4776746 > 60004 4843949 > what i want is quartile values of price grouped by zip >-- View this message in context: http://www.nabble.com/use-of-class-variable-in-r-as-in-Proc-means-of-sas-tp25530654p25530997.html Sent from the R help mailing list archive at Nabble.com.
See if this works: qfun2 <- function(x, digits=3,sci=F,...){ c(q=quantile(x, probs=c(1,5,10,95,99)/100,type=6,...) ) } cheers, -Girish ========================== premmad wrote:> > I tried thanks for your help and got the same result for percentile 5 & 95 > as in SAS.But if i need to calculate quantiles (1,5,10,99,etc.) it will > not be possible with fivenum as explained in the help page .If i need > those quantiles what is the change i need to make in the function > qfu<-function(x,digits=3,sci=F,...) > {c(q=quantile(x,probs=c(5,90)/100)) > } > > or do i need to include some other function . > >-- View this message in context: http://www.nabble.com/use-of-class-variable-in-r-as-in-Proc-means-of-sas-tp25530654p25531097.html Sent from the R help mailing list archive at Nabble.com.
Ya it works thanks for the help -- View this message in context: http://www.nabble.com/use-of-class-variable-in-r-as-in-Proc-means-of-sas-tp25530654p25531102.html Sent from the R help mailing list archive at Nabble.com.