[R] Why does "summary" show number of NAs as non-integer?

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Jun 1 01:32:05 CEST 2005


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?
> 
> efg

This 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




More information about the R-help mailing list