我的类型是什么?
本练习是课程的一部分
R 中的 S3 与 R6 面向对象编程
练习说明
工作区中已预定义 type_info() 函数,用于返回其输入的 class()、mode()、typeof() 和 storage.mode()。(在控制台输入 type_info 可查看其实现方式。)
- 创建
some_vars,即编辑器中提供的示例对象列表。 - 使用
lapply遍历some_vars的各个元素,对每个示例对象调用type_info(),以查看其类型信息。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Look at the definition of type_info()
type_info
# Create list of example variables
some_vars <- list(
an_integer_vector = rpois(24, lambda = 5),
a_numeric_vector = rbeta(24, shape1 = 1, shape2 = 1),
an_integer_array = array(rbinom(24, size = 8, prob = 0.5), dim = c(2, 3, 4)),
a_numeric_array = array(rweibull(24, shape = 1, scale = 1), dim = c(2, 3, 4)),
a_data_frame = data.frame(int = rgeom(24, prob = 0.5), num = runif(24)),
a_factor = factor(month.abb),
a_formula = y ~ x,
a_closure_function = mean,
a_builtin_function = length,
a_special_function = `if`
)
# Loop over some_vars calling type_info() on each element to explore them
___