对R中事物类型的全面考察; “模式”和“类”和“类型”;不足
R 语言让我很困惑。实体有模式和类,但即使这样也不足以完全描述实体。
这个答案说
在 R 中,每个“对象”都有一个模式和一个类。
所以我做了这些实验:
> class(3)
[1] "numeric"
> mode(3)
[1] "numeric"
> typeof(3)
[1] "double"
到目前为止还不错,但后来我传入了一个向量:
> mode(c(1,2))
[1] "numeric"
> class(c(1,2))
[1] "numeric"
> typeof(c(1,2))
[1] "double"
这没有意义。整数向量肯定应该具有与单个整数不同的类或不同的模式吗?我的问题是:
- R 中的所有内容是否都有(恰好一个)类?
- R 中的所有内容都具有(恰好一个)模式 吗?
- 如果有的话,“typeof”告诉我们什么?
- 完整描述一个实体还需要哪些其他信息? (例如,“向量”存储在哪里?)
更新:显然,文字 3 只是长度为 1 的向量。没有标量。好的但是...我尝试了 mode("string")
并得到了 "character"
,这让我认为字符串是字符向量。但如果这是真的,那么这应该是真的,但事实并非如此! c('h','i') == "嗨"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我同意 R 中的类型系统相当奇怪。之所以会这样,是因为它已经发展了(很长一段)时间...
请注意,您错过了又一个类似类型的函数,
storage.mode
,以及又一个类似类的函数,oldClass
。因此,
mode
和storage.mode
是旧式类型(其中storage.mode
更准确),而typeof< /code> 是更新、更准确的版本。
那么
class
就是一个完全不同的故事。class
主要只是对象的class
属性(这正是oldClass
返回的内容)。但是当没有设置class属性时,class
函数会根据对象类型和dim属性组成一个类。最后,类可以返回多个字符串,但前提是类属性是这样的。第一个字符串值是主类的一种,下面的值是它继承的内容。补课的长度始终为 1。
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.
下面是一些代码,用于确定四种类型函数,class,模式,typeof 和 storage.mode 返回每种类型的 R 对象。
以下是输出:
R 中可用的对象类型在 R 语言定义手册。这里没有提到一些类型:您无法测试“promise”、“...”和“ANY”类型的对象,并且“bytecode”和“weakref”仅在 C 级别可用。
R 源中可用类型的表为 在这里。
Here's some code to determine what the four type functions, class, mode, typeof, and storage.mode return for each of the kinds of R object.
Here's the output:
The types of objects available in R are discussed in the R Language Definition manual. There are a few types not mentioned here: you can't test for objects of type "promise", "...", and "ANY", and "bytecode" and "weakref" are only available at the C-level.
The table of available types in the R source is here.
R 中的所有内容都具有(恰好一个)类吗?
恰好一个肯定是不对的:
所有内容都具有(至少一个)类。
R 中的所有内容都具有(恰好一种)模式吗?
不确定,但我怀疑是这样。
“typeof”告诉我们什么(如果有的话)?
typeof
给出对象的内部类型。根据?typeof
的可能值为:mode
依赖于 typeof。来自?mode
:完整描述一个实体还需要哪些信息?(例如,“列表”存储在哪里?)
列表具有类list:
您的意思是矢量化吗?
length
应该足以满足大多数用途:视为长度为 1 的数值向量,而不是某种原始数值类型。
将
3
>结论你可以过得很好
class
和length
当您需要其他东西时,您可能不必问它们的用途:-)Does everything in R have (exactly one) class ?
Exactly one is definitely not right:
Everything has (at least one) class.
Does everything in R have (exactly one) mode ?
Not certain but I suspect so.
What, if anything, does 'typeof' tell us?
typeof
gives the internal type of an object. Possible values according to?typeof
are:mode
relies on typeof. From?mode
:What other information is needed to fully describe an entity? (Where is the 'listness' stored, for example?)
A list has class list:
Do you mean vectorization?
length
should be sufficient for most purposes:Think of
3
as a numeric vector of length 1, rather than as some primitive numeric type.Conclusion
You can get by just fine with
class
andlength
. By the time you need the other stuff, you likely won't have to ask what they're for :-)添加到您的子问题之一:
另外还有
class
、mode
、typeof
、attributes
、str
等等另外,is()
也值得注意。虽然有用,但也不尽如人意。在此示例中,
1
不仅仅如此;它也是原子的、有限的和双精度的。以下函数应该根据所有可用的is.(...)
函数显示对象的所有信息:现在我们得到了更完整的图片:
您还可以通过参数
获得更多信息show.all=TRUE
。我不会在此处粘贴任何示例,因为结果超过 50 行长。最后,这意味着作为信息的补充来源,而不是作为前面提到的任何其他功能的替代。
编辑
要包含更多“is”函数,根据@Erdogan的评论,您可以将此位添加到函数中:
Adding to one of your sub-questions :
In addition to
class
,mode
,typeof
,attributes
,str
, and so on,is()
is also worth noting.While useful, it is also unsatisfactory. In this example,
1
is more than just that; it is also atomic, finite, and a double. The following function should show all that an object is according to all availableis.(...)
functions:So now we get a more complete picture:
You also get more information with the argument
show.all=TRUE
. I am not pasting any example here as the results are over 50 lines long.Finally, this is meant as a complementary source of information, not as a replacement for any of the other functions mentionned earlier.
EDIT
To include even more "is" functions, as per @Erdogan's comment, you could add this bit to the function:
这个问题涉及 R 中各种“类型”的混乱。在阅读了这篇文章和其他一些网站之后,我认为 @RichieCotton 的一条评论最好地解决了这种混乱。我花了很长时间才遇到这个评论,我想通过将其添加为答案来突出显示它。
[...] 此时,
mode
和storage.mode
是 S 遗留下来的遗留功能。您应该只需要关心class()
和typeof()
。因此,在阅读其他答案时请记住这一点,并重点关注
typeof
和class.不幸的是,在其他帖子中,不应该使用
mode
和storage.mode
并不明显。首先阅读此评论可以节省我很多时间。This question deals with the confusion around all kinds of "types" in R. After reading this and quite a few other sites, in my opinion one comment by @RichieCotton best solves the confusion. It took me quite a while to encounter this comment and I want to highlight it by adding it as an answer.
[...] at this point,
mode
andstorage.mode
are legacy features left over from S. You should only ever need to care aboutclass()
andtypeof()
.So while reading other answers keep this in mind and focus on
typeof
andclass
. Unfortunately, in other posts it does not become obvious thatmode
andstorage.mode
should not be used. It would have saved me a lot of time reading this comment first.