[R] min(NA,"bla") != min("bla", NA)

Marc Schwartz marc_schwartz at me.com
Thu Sep 26 18:15:12 CEST 2013


Arun,

This additional example is still irrelevant.

The difference occurs when the two values are passed as the "..." argument to min(), which is a list and is what Magnus was using, versus your examples, where the two values are passed as a single vector, which is coerced to character:

> str(c(NA, "bla"))
 chr [1:2] NA "bla"

as compared to:

> str(list(NA, "bla"))
List of 2
 $ : logi NA
 $ : chr "bla"


Unless there is something in the internal C code, since min() is a .Primitive, where the lexical ordering of the two values NA and "bla" are relevant to the processing of the values and the returned value (eg. a collating sequence issue or a hierarchy of coercion issue), this appears to be a possible bug.

NOTE:

# Returns a int
> str(min(NA, "bla"))
 int NA

# Returns a character
> str(min("bla", NA))
 chr "bla"


However:

# Both return characters
> str(min(as.character(NA), "bla"))
 chr "bla"

> str(min("bla", as.character(NA)))
 chr "bla"


It would appear that in Magnus' example, that NA is passed as a logical in the argument list may be relevant here.

BTW, I can reproduce Magnus' example on 3.0.2 on OSX (10.8.5).

Regards,

Marc Schwartz


On Sep 26, 2013, at 10:40 AM, arun <smartpink111 at yahoo.com> wrote:

> min(c(NA,"bla"),na.rm=FALSE)
> [1] NA
>> min(c("bla",NA),na.rm=FALSE)
> [1] NA
> A.K.
> 
> 
> 
> ----- Original Message -----
> From: Ista Zahn <istazahn at gmail.com>
> To: arun <smartpink111 at yahoo.com>
> Cc: Magnus Thor Torfason <zulutime.net at gmail.com>; R help <r-help at r-project.org>
> Sent: Thursday, September 26, 2013 11:36 AM
> Subject: Re: [R] min(NA,"bla") != min("bla", NA)
> 
> Hi A.K.,
> 
> On Thu, Sep 26, 2013 at 11:22 AM, arun <smartpink111 at yahoo.com> wrote:
>> Hi,
>>   min(5,1)
>> #[1] 1
>> 
>> 
>>   min(c(1,5))
>> #[1] 1
>>   min(c(1,5))==min(c(5,1))
>> #[1] TRUE
>> 
>> 
>> 
>>    min(c(NA,"bla"))
>> #[1] NA
>>   min(c("bla",NA))
>> #[1] NA
>>   min(c("bla",NA),na.rm=FALSE)
>> #[1] NA
>>   min(c("bla",NA),na.rm=TRUE)
>> #[1] "bla"
> 
> What is the point of this example? The OP's point was (I hope I am not
> putting words in his mouth) that the documentation for ?min says
> 
> "     If ‘na.rm’ is ‘FALSE’ an ‘NA’ value in any of the arguments will
>      cause a value of ‘NA’ to be returned, otherwise ‘NA’ values are
>      ignored."
> 
> but that appears not to be true:
> 
> min("bla", NA)
> [1] "bla"
> 
> It's not clear to me how your example is relevant.
> 
> Best,
> Ista
> 
>> 
>> A.K.
>> 
>> 
>> 
>> ----- Original Message -----
>> From: Magnus Thor Torfason <zulutime.net at gmail.com>
>> To: "r-help at r-project.org" <r-help at r-project.org>
>> Cc:
>> Sent: Thursday, September 26, 2013 11:07 AM
>> Subject: [R] min(NA,"bla") != min("bla", NA)
>> 
>> Just ran these two statements:
>> 
>>> min(NA,"bla")
>> [1] NA
>> 
>>> min("bla", NA)
>> [1] "bla"
>> 
>> And then reran with explicit na.rm=FALSE
>> 
>>> min(NA,"bla", na.rm=FALSE)
>> [1] NA
>> 
>>> min("bla", NA, na.rm=FALSE)
>> [1] "bla"
>> 
>> 
>> That seems wrong. Would this be considered a bug or is there a way to
>> explain these results in a different way?
>> 
>> Best,
>> Magnus
>> 
>> ps. Tested on R 3.0.1, 32 bit for Windows (as well as some older versions)



More information about the R-help mailing list