English · PDF 336
接下来将介绍这些内容。
因子用于表示可以取一组固定可能值的分类数据。因子建立在整数之上,并具有一个 levels(水平)属性:
x <- factor(c("ab", "cd", "ab"), levels = c("ab", "cd", "ef")) typeof(x) #> [1] "integer" attributes(x) #> $levels #> [1] "ab" "cd" "ef" #> #> $class #> [1] "factor"
R 中的日期是数值向量,表示自 1970 年 1 月 1 日以来的天数:
x <- as.Date("1971-01-01") unclass(x) #> [1] 365 typeof(x) #> [1] "double" attributes(x) #> $class #> [1] "Date"
日期时间是具有 POSIXct 类的数值向量,表示自 1970 年 1 月 1 日以来的秒数。(如果你好奇的话,"POSIXct" 代表 "Portable Operating System Interface"(可移植操作系统接口),calendar time(日历时间)。)
x <- lubridate::ymd_hm("1970-01-01 01:00") unclass(x) #> [1] 3600 #> attr(,"tzone") #> [1] "UTC"