Dear R users, Something is strange in summary and IQR. Suppose, I have a data set and I would like to find the Q1, Q2, Q3 and IQR. x<-c(2,4,11,12,13,15,31,31,37,47)> summary(x)Min. 1st Qu. Median Mean 3rd Qu. Max. 2.00 11.25 14.00 20.30 31.00 47.00> IQR(x)[1] 19.75 However, I test the same data set in SAS "proc univariate", and SAS shows that Q1=11, Q2=14 and Q3=31. I think most of us agree that Q1 is 11 not 11.25. Could someone please explain to me why R shows Q1=11.25 not 11? Many Thanks Tu -- View this message in context: http://www.nabble.com/strange-results-in-summary-and-IQR-functions-tp25348079p25348079.html Sent from the R help mailing list archive at Nabble.com.
It's all simply a matter of definitions, and there are many who disagree. See ?quantile , specifically the "type" argument. Since IQR does not appear to have a type argument, you could easily write your own versions of these that do what SAS does (assuming that is your goal). With x defined as you have it, look at the results of this function call, which shows the different values for quantile that you get by using different "type" arguments.> sapply(1:9, function(y) quantile(x, type = y))[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 0% 2 2 2 2.0 2 2.00 2.00 2.00000 2.0000 25% 11 11 4 7.5 11 9.25 11.25 10.41667 10.5625 50% 13 14 13 13.0 14 14.00 14.00 14.00000 14.0000 75% 31 31 31 31.0 31 32.50 31.00 31.50000 31.3750 100% 47 47 47 47.0 47 47.00 47.00 47.00000 47.0000 Best, Erik Iverson -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Chunhao Tu Sent: Tuesday, September 08, 2009 10:09 AM To: r-help at r-project.org Subject: [R] strange results in summary and IQR functions Dear R users, Something is strange in summary and IQR. Suppose, I have a data set and I would like to find the Q1, Q2, Q3 and IQR. x<-c(2,4,11,12,13,15,31,31,37,47)> summary(x)Min. 1st Qu. Median Mean 3rd Qu. Max. 2.00 11.25 14.00 20.30 31.00 47.00> IQR(x)[1] 19.75 However, I test the same data set in SAS "proc univariate", and SAS shows that Q1=11, Q2=14 and Q3=31. I think most of us agree that Q1 is 11 not 11.25. Could someone please explain to me why R shows Q1=11.25 not 11? Many Thanks Tu -- View this message in context: http://www.nabble.com/strange-results-in-summary-and-IQR-functions-tp25348079p25348079.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.
Have a look at ?IQR "Note that this function computes the quartiles using the quantile function rather than following Tukey's recommendations, i.e., IQR(x) = quantile(x,3/4) - quantile(x,1/4)." It looks like boxplot() gives the results you expect. tt <- boxplot(x) tt --- On Tue, 9/8/09, Chunhao Tu <tu_chunhao at yahoo.com> wrote:> From: Chunhao Tu <tu_chunhao at yahoo.com> > Subject: [R] strange results in summary and IQR functions > To: r-help at r-project.org > Received: Tuesday, September 8, 2009, 11:09 AM > > Dear R users, > Something is strange in summary and IQR. Suppose, I have a > data set and I > would like to find the Q1, Q2, Q3 and IQR.? > > x<-c(2,4,11,12,13,15,31,31,37,47) > > summary(x) > ???Min. 1st Qu.? Median? ? > Mean 3rd Qu.? ? Max. > ???2.00???11.25???14.00???20.30???31.00???47.00 > > > IQR(x) > [1] 19.75 > However, I test the same data set in SAS "proc univariate", > and SAS shows > that Q1=11, Q2=14 and Q3=31. I think most of us agree that > Q1 is 11 not > 11.25. > > Could someone please explain to me why R shows Q1=11.25 not > 11? > > Many Thanks > Tu > > > -- > View this message in context: http://www.nabble.com/strange-results-in-summary-and-IQR-functions-tp25348079p25348079.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. >__________________________________________________________________ Looking for the perfect gift? Give the gift of Flickr! http://www.flickr.com/gift/
Maybe Matching Threads
- quantile and IQR do not check for numeric input (PR#13631)
- 95% CI of a IQR and more
- quantile(), IQR() and median() for factors
- Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...
- Something wrong with my function Please Help