Earl F. Glynn
2005-May-31 22:14 UTC
[R] Why does "summary" show number of NAs as non-integer?
Example:> set.seed(19) > summary( c(NA, runif(10,1,100), NaN) )Min. 1st Qu. Median Mean 3rd Qu. Max. NA's 7.771 24.850 43.040 43.940 63.540 83.830 2.000 Why isn't the number of NA's just "2" instead of the "2.000" shown above? efg
Berton Gunter
2005-May-31 22:40 UTC
[R] Why does "summary" show number of NAs as non-integer?
summary() is an S3 generic that for your vector dispatches summary.default(). The output of summary default has class "table" and so calls print.table (print is another S3 generic). Look at the code of print.table() to see how it formats the output. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Earl F. Glynn > Sent: Tuesday, May 31, 2005 3:14 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Why does "summary" show number of NAs as non-integer? > > Example: > > > set.seed(19) > > summary( c(NA, runif(10,1,100), NaN) ) > Min. 1st Qu. Median Mean 3rd Qu. Max. NA's > 7.771 24.850 43.040 43.940 63.540 83.830 2.000 > > Why isn't the number of NA's just "2" instead of the "2.000" > shown above? > > efg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Marc Schwartz
2005-May-31 23:32 UTC
[R] Why does "summary" show number of NAs as non-integer?
On Tue, 2005-05-31 at 17:14 -0500, Earl F. Glynn wrote:> Example: > > > set.seed(19) > > summary( c(NA, runif(10,1,100), NaN) ) > Min. 1st Qu. Median Mean 3rd Qu. Max. NA's > 7.771 24.850 43.040 43.940 63.540 83.830 2.000 > > Why isn't the number of NA's just "2" instead of the "2.000" shown above? > > efgThis is actually related to the thread on formatting numbers. In reviewing the Detail section of ?print.default: "The same number of decimal places is used throughout a vector, This means that digits specifies the minimum number of significant digits to be used, and that at least one entry will be printed with that minimum number." 'digits' in the above is the digits argument to print.default(). In this case, it defaults to options("digits"), which is 7. In the above output from summary, you will note that all of the output has three digits after the decimal place. Thus:> c(2)[1] 2> c(2, 3)[1] 2 3> c(2, 3.5)[1] 2.0 3.5> c(2, 3.57)[1] 2.00 3.57> c(2, 3.579)[1] 2.000 3.579 Note how the output format of "2" varies depending upon how many decimal places I use in the second element. This goes to the need to use other functions where there is a need to exert greater control over how numeric output can be formatted and aligned using formatC() and/or sprintf(). For example:> sprintf("0 decimal places: %d 3 decimal places: %4.3f", 2, 3.57911)[1] "0 decimal places: 2 3 decimal places: 3.579" See ?sprintf and ?formatC for more information. HTH, Marc Schwartz