From murdoch.duncan at gmail.com  Sat Oct 15 12:41:08 2016
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Sat, 15 Oct 2016 06:41:08 -0400
Subject: [Rd] Inconsistent behavior of summary
In-Reply-To: <CAOHtMfWg2ayM0XFma3kxuiJ_Uz1WjW4XfSQ_y=P4jtqo0WcbBQ@mail.gmail.com>
References: <CAOHtMfWg2ayM0XFma3kxuiJ_Uz1WjW4XfSQ_y=P4jtqo0WcbBQ@mail.gmail.com>
Message-ID: <c7d13f14-74ca-c841-0b82-743a9dc3a609@gmail.com>

On 15/10/2016 5:48 AM, Kaiyin Zhong wrote:
> Here is the code:
>
> summary(c(1:5, NA))
> summary(c("a", NA))
>
> It seems in the firs case the number of NAs is reported, but not in the
> second.
>
> Tested with R 3.3.1 on a mac.

summary() is a generic function, so the output depends on the class of 
the input.  There's no special method for character vectors, so you get 
the default output.  Numeric vectors show more detail.

If you want a particular behaviour for character objects, just write a 
summary.character method.

Here's the default:

 > summary(c("a", NA))
    Length     Class      Mode
         2 character character

Here's a method and example using it:

 > summary.character <- function (x, ...) { cat("this character vector 
has", sum(is.na(x)), " NA value(s)\n")}

 > summary(c("a", NA))
this character vector has 1  NA value(s)

Duncan Murdoch



[Rd] Inconsistent behavior of summary
Duncan Murdoch