0Pricing
R Academy · 课时

R 类型系统概览

了解 double、integer、character、logical 和 complex 类型

R 类型系统概览 是 CoddyKit 上的免费 R Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 R Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 R Academy 课程共包含 4 节课。

R 的类型系统

R 拥有丰富的类型系统。每个对象都有一个类别(高层类别)和一个类型(底层存储模式)。理解这两者有助于您编写正确、高效的代码,并调试与类型相关的错误。

# Check class and type of various objects
cat('class(42L)    :', class(42L), '
')
cat('typeof(42L)   :', typeof(42L), '
')
cat('class(3.14)   :', class(3.14), '
')
cat('typeof(3.14)  :', typeof(3.14), '
')
cat('class(TRUE)   :', class(TRUE), '
')
cat('class("hello"):', class('hello'), '
')

class() 与 typeof()

class(x) 返回 R 面向对象系统使用的高层类别(例如 'integer'、'numeric'、'matrix')。typeof(x) 返回底层的 C 存储类型(例如 'integer'、'double'、'closure')。

m <- matrix(1:4, nrow = 2)
cat('class(matrix)  :', class(m), '
')   # matrix array
cat('typeof(matrix) :', typeof(m), '
')  # integer

f <- factor(c('a', 'b', 'a'))
cat('class(factor)  :', class(f), '
')   # factor
cat('typeof(factor) :', typeof(f), '
')  # integer (stored as int!)

cat('class(list)    :', class(list()), '
')  # list
cat('typeof(list)   :', typeof(list()), '
') # list

is.numeric() — 数值检查

is.numeric(x) 对 integer 和 double 值(以及数值矩阵)都会返回 TRUE。它检查的是数值超类型,而不是具体的存储模式。

cat('is.numeric(3.14):', is.numeric(3.14), '
')    # TRUE (double)
cat('is.numeric(3L):  ', is.numeric(3L), '
')      # TRUE (integer)
cat('is.numeric(TRUE):', is.numeric(TRUE), '
')    # FALSE
cat('is.numeric("3"): ', is.numeric('3'), '
')     # FALSE
cat('is.numeric(NA):  ', is.numeric(NA), '
')      # FALSE (NA is logical)

is.integer() 与 is.double()

is.integer() 专门检查整数存储(通过 L 后缀或 as.integer() 创建)。is.double() 检查浮点数(double)存储。两者都属于 numeric 的子集。

x_int <- 5L
x_dbl <- 5.0

cat('--- x_int = 5L ---
')
cat('is.integer:', is.integer(x_int), '
')  # TRUE
cat('is.double: ', is.double(x_int), '
')   # FALSE
cat('is.numeric:', is.numeric(x_int), '
')  # TRUE

cat('--- x_dbl = 5.0 ---
')
cat('is.integer:', is.integer(x_dbl), '
')  # FALSE
cat('is.double: ', is.double(x_dbl), '
')   # TRUE
cat('is.numeric:', is.numeric(x_dbl), '
')  # TRUE

is.character() — 字符串检查

如果 x 是字符(字符串)向量,is.character(x) 就会返回 TRUE。您可以用它验证应为文本的输入,或区分数值型和字符型的 NA。

greetings <- c('hello', 'world', 'R')
numbers <- c(1, 2, 3)
numeric_string <- '42'

cat('is.character(greetings):      ', is.character(greetings), '
')
cat('is.character(numbers):        ', is.character(numbers), '
')
cat('is.character(numeric_string): ', is.character(numeric_string), '
')
cat('is.character(NA_character_):  ', is.character(NA_character_), '
')

is.logical() — 布尔值检查

如果 x 包含逻辑值(布尔值),is.logical(x) 就会返回 TRUE。请注意,未指定类型的 NA 默认是逻辑值。

flags <- c(TRUE, FALSE, TRUE, NA)
cat('is.logical(flags): ', is.logical(flags), '
')   # TRUE
cat('is.logical(NA):    ', is.logical(NA), '
')      # TRUE (default NA is logical)
cat('is.logical(1):     ', is.logical(1), '
')       # FALSE
cat('is.logical("TRUE"):', is.logical('TRUE'), '
')  # FALSE
cat('typeof(NA):        ', typeof(NA), '
')          # logical

R 的类型层次结构

R 的原子类型从简单到复杂构成如下层次:logical < integer < double < complex < character。在向量中混合不同类型时,R 会将所有元素强制转换为其中存在的最复杂类型。

# Type hierarchy in action
cat('c(TRUE, 1L) type:     ', typeof(c(TRUE, 1L)), '
')   # integer
cat('c(1L, 1.5) type:      ', typeof(c(1L, 1.5)), '
')   # double
cat('c(1.5, 1+0i) type:    ', typeof(c(1.5, 1+0i)), '
') # complex
cat('c(1+0i, "a") type:   ', typeof(c(1+0i, 'a')), '
')  # character
cat('c(TRUE, "x") type:    ', typeof(c(TRUE, 'x')), '
') # character

以编程方式检查类型

您可以编写类型检查函数来审查对象列表。结合使用 class()、typeof() 和 length(),即可全面概括任意对象的结构。

describe_type <- function(x, label) {
  cat(label, ': class =', class(x), ', typeof =', typeof(x),
      ', length =', length(x), '
')
}

describe_type(42L,         'integer literal')
describe_type(3.14,        'double literal')
describe_type(TRUE,        'logical')
describe_type('hello',     'character')
describe_type(c(1,2,3),    'numeric vector')
describe_type(list(1,'a'), 'list')

storage.mode() 与 mode()

storage.mode(x) 与 typeof(x) 类似,但采用 S3 命名惯例(例如返回 'double' 而不是 'double')。mode(x) 返回更粗略的分组,会将 integer 和 double 归为 'numeric'。

x_int <- 5L
x_dbl <- 5.0

cat('mode(5L):         ', mode(x_int), '
')          # numeric
cat('mode(5.0):        ', mode(x_dbl), '
')          # numeric
cat('storage.mode(5L): ', storage.mode(x_int), '
')  # integer
cat('storage.mode(5.0):', storage.mode(x_dbl), '
')  # double
# mode() lumps int+dbl; storage.mode() / typeof() distinguish them

列表和数据框的类型检查

对于列表和数据框等更复杂的对象,sapply(df, class) 会对每一列应用 class(),返回一个带名称的列类型向量——这是快速审查数据框结构的方式。

df <- data.frame(
  id      = 1:3,
  name    = c('Alice', 'Bob', 'Carol'),
  score   = c(88.5, 79.0, 92.3),
  passed  = c(TRUE, TRUE, TRUE)
)
cat('Column types:
')
print(sapply(df, class))
cat('Column typeof:
')
print(sapply(df, typeof))

类型系统总结

下面是 R 类型检查工具的快速参考:

  • class(x) — 高层 S3 类名称
  • typeof(x) — 底层 C 存储类型
  • is.numeric(x) — 对 integer 或 double 返回 TRUE
  • is.integer(x) — 仅对整数存储返回 TRUE
  • is.double(x) — 仅对 double(浮点数)存储返回 TRUE
  • is.character(x) — 对字符串返回 TRUE
  • is.logical(x) — 对 TRUE/FALSE/NA 返回 TRUE
x <- 42L
cat('class(42L):       ', class(x), '
')
cat('typeof(42L):      ', typeof(x), '
')
cat('is.numeric(42L):  ', is.numeric(x), '
')
cat('is.integer(42L):  ', is.integer(x), '
')
cat('is.double(42L):   ', is.double(x), '
')
cat('is.character(42L):', is.character(x), '
')

快速检查

在 R 中,is.numeric(5L) 会返回什么?

回顾:R 的类型系统

做得很好!本课的要点如下:

  • class(x) 返回高层类型;typeof(x) 返回底层存储类型
  • is.numeric() 对整数和 double 都返回 TRUE
  • is.integer() 和 is.double() 用于区分这两种数值子类型
  • R 的类型层次结构(从低到高):logical → integer → double → complex → character
  • 在向量中混合类型会触发自动强制转换,转换为最高级的类型
  • sapply(df, class) 可以审查数据框中的所有列类型
# Type audit function
audit_types <- function(x, name = 'x') {
  cat(name, ': class=', class(x), ', typeof=', typeof(x),
      ', is.numeric=', is.numeric(x), '
')
}
audit_types(1L,      'integer')
audit_types(1.0,     'double')
audit_types(TRUE,    'logical')
audit_types('hello', 'character')
audit_types(1+2i,    'complex')

常见问题解答

「R 类型系统概览」课时是免费的吗?

是的 — 「R 类型系统概览」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 R Academy 课程的其余内容,请升级到 CoddyKit PRO。 R Academy 课程共包含 4 节课。

「R 类型系统概览」这节课中我会学到什么?

了解 double、integer、character、logical 和 complex 类型 你通过在浏览器中直接运行的动手代码来练习 R Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 R Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 R Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「R 类型系统概览」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 R Academy 课中编写并运行代码吗?

能。每节 R Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. R 类型系统概览
  2. 在数值类型之间转换
  3. 逻辑类型与字符类型转换
  4. 强制类型转换的陷阱与最佳实践
← 返回 R Academy