The language R confuses me. Entities have modes and classes, but even this is insufficient to fully describe the entity.
This answer says
In R every 'object' has a mode and a class.
So I did these experiments:
> class(3)
[1] "numeric"
> mode(3)
[1] "numeric"
> typeof(3)
[1] "double"
Fair enough so far, but then I passed in a vector instead:
> mode(c(1,2))
[1] "numeric"
> class(c(1,2))
[1] "numeric"
> typeof(c(1,2))
[1] "double"
That doesn't make sense. Surely a vector of integers should have a different class, or different mode, than a single integer? My questions are:
- Does everything in R have (exactly one) class ?
- Does everything in R have (exactly one) mode ?
- What, if anything, does 'typeof' tell us?
- What other information is needed to fully describe an entity? (Where is the 'vectorness' stored, for example?)
Update: Apparently, a literal 3 is just a vector of length 1. There are no scalars. OK But... I tried mode("string")
and got "character"
, leading me to think that a string was a vector of characters. But if that was true, then this should be true, but it's not! c('h','i') == "hi"
I agree that the type system in R is rather weird. The reason for it being that way is that it has evolved over (a long) time...
Note that you missed one more type-like function,
storage.mode
, and one more class-like function,oldClass
.So,
mode
andstorage.mode
are the old-style types (wherestorage.mode
is more accurate), andtypeof
is the newer, even more accurate version.Then
class
is a whole different story.class
is mostly just theclass
attribute of an object (that's exactly whatoldClass
returns). But when the class attribute is not set, theclass
function makes up a class from the object type and the dim attribute.Finally, the class can return more than one string, but only if the class attribute is like that. The first string value is then kind of the main class, and the following ones are what it inherits from. The made-up classes are always of length 1.