gpt4 book ai didi

class - 变量的类型和类别

转载 作者:行者123 更新时间:2023-12-03 05:07:21 26 4
gpt4 key购买 nike

两个 R 问题:

  1. 变量的类型(由 typeof 返回)和类(由 class 返回)有什么区别?这种差异与 C++ 语言中的差异类似吗?
  2. 变量可能的类型和类别有哪些?

最佳答案

在 R 中,每个“对象”都有一个模式和一个。前者表示对象在内存中的存储方式(数字、字符、列表和函数),而后者表示其抽象类型。例如:

d <- data.frame(V1=c(1,2))
class(d)
# [1] "data.frame"
mode(d)
# [1] "list"
typeof(d)
# list

正如您所看到的,数据帧作为 list 存储在内存中,但它们被包装到 data.frame 对象中。后者允许使用成员函数以及重载函数,例如具有自定义行为的 print

typeof(storage.mode) 通常会提供与 mode 相同的信息,但并非总是如此。例证:

typeof(c(1,2))
# [1] "double"
mode(c(1,2))
# [1] "numeric"

这背后的原因可以在 here 找到:

The R specific function typeof returns the type of an R object

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

我上面发布的链接还包含所有 native R 基本类型(向量、列表等)和所有复合对象(因子和 data.frames)的列表)以及 modetypeofclass 与每种类型如何相关的一些示例。

关于class - 变量的类型和类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6258004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com