变量的类型和类别 [英] Types and classes of variables
问题描述
两个R问题:
- 类型之间有什么区别(由
typeof $ c返回) $ c>)和变量的类(由
class
返回)?区别是否类似于C ++语言? - 变量的可能类型和类别是什么?
- What is the difference between the type (returned by
typeof
) and the class (returned byclass
) of a variable? Is the difference similar to that in, say, C++ language? - What are possible types and classes of variables?
推荐答案
R中的每个对象具有模式
和类
。前者表示对象在内存中的存储方式(数字,字符,列表和函数),而后者则表示其抽象类型。例如:
In R every "object" has a mode
and a class
. The former represents how an object is stored in memory (numeric, character, list and function) while the later represents its abstract type. For example:
d <- data.frame(V1=c(1,2))
class(d)
# [1] "data.frame"
mode(d)
# [1] "list"
typeof(d)
# list
您可以看到数据帧存储为列表
,但它们被包装到 data.frame
对象中。后者允许使用成员函数以及具有自定义行为的重载函数,例如 print
。
As you can see data frames are stored in memory as list
but they are wrapped into data.frame
objects. The latter allows for usage of member functions as well as overloading functions such as print
with a custom behavior.
typeof
( storage.mode
)通常会提供与 mode
相同的信息,但不会总是。适当的例子:
typeof
(storage.mode
) will usually give the same information as mode
but not always. Case in point:
typeof(c(1,2))
# [1] "double"
mode(c(1,2))
# [1] "numeric"
其原因可以在此处:
特定于R函数 typeof 返回R对象的类型
函数 mode 给出贝克尔意义上的对象模式信息,钱伯斯Wilks(1988),并且与S语言的其他实现更兼容
Function mode gives information about the mode of an object in the sense of Becker, Chambers & Wilks (1988), and is more compatible with other implementations of the S language
我在上面发布的链接还包含所有本机R 基本类型
(向量,列表等)和所有复合对象
(因子和data.frames)以及一些示例模式
, typeof
和 class
的关系类型。
The link that I posted above also contains a list of all native R basic types
(vectors, lists etc.) and all compound objects
(factors and data.frames) as well as some examples of how mode
, typeof
and class
are related for each type.
这篇关于变量的类型和类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!