[Rd] Discrepancy between is.list() and is(x, "list")

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Mar 26 18:51:40 CET 2019


Hello,

Here is another example.

df1 <- data.frame(a = 1:3, b = 4:6)

inherits(df1, "data.frame")
#[1] TRUE
class(df1)
#[1] "data.frame"

inherits(df1, "list")
#[1] FALSE


This is documented behavior, the help page ?inherits says

The function class prints the vector of names of classes an object 
inherits from.


So far, so good. But now comes the part I don't like.

is.list(df1)
#[1] TRUE


Strictly speaking this is not unexpected behavior (because it's 
documented) but isn't it *inconsistent* behavior?


Rui Barradas

Às 16:30 de 26/03/2019, Berry, Charles escreveu:
> In the case of inherits (at least) this seems intended.
> 
> The help page says:
> 
> "If the object does not have a class attribute, it has an implicit class..."
> 
> which I take to mean that if an object does have a class attribute it does not also have an implicit class.
> 
> The behavior you noted below will apply to other types bearing implicit classes. For example:
> 
>> inherits(1.0, "numeric")
> [1] TRUE
>> inherits(structure(1.0, class="myclass"), "numeric")
> [1] FALSE
>>
> 
> I think this is reasonable behavior. Consider the "Date" class, which stores values as "numeric":
> 
>> class(Sys.Date())
> [1] "Date"
>> inherits(Sys.Date(),"numeric")
> [1] FALSE
>> class(unclass(Sys.Date()))
> [1] "numeric"
>> Sys.Date()%%2
> Error in Ops.Date(Sys.Date(), 2) : %% not defined for "Date" objects
>>
> 
> Letting the modulus operator (as one example) inherit the numeric class here could create problems.
> 
> Of course for classes that should inherit the implicit type, it can be explicitly added to the end of the class() vector by its constructor.
> 
> HTH,
> 
> Chuck
> 
> 
> 
>> On Mar 25, 2019, at 8:27 PM, Abs Spurdle <spurdle.a using gmail.com> wrote:
>>
>>> I have noticed a discrepancy between is.list() and is(x, “list”)
>>
>> There's a similar problem with inherits().
>>
>> On R 3.5.3:
>>
>>> f = function () 1
>>> class (f) = "f"
>>
>>> is.function (f)
>> [1] TRUE
>>> inherits (f, "function")
>> [1] FALSE
>>
>> I didn't check what happens with:
>>> class (f) = c ("f", "function")
>>
>> However, they should have the same result, regardless.
>>
>>> Is this discrepancy intentional?
>>
>> I hope not.
>>
>> 	[[alternative HTML version deleted]]
>>
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list