data.table
做了一些花哨的步法,这意味着在对象上data.table:::.rbind.data.table
调用时rbind
调用,包括data.tables
. .rbind.data.table
利用与 相关的加速rbindlist
,并进行一些额外的检查以按名称匹配等。
.rbind.data.table
通过组合它们来处理因子列c
(因此保留了级别属性)
# the relevant code is
l = lapply(seq_along(allargs[[1L]]), function(i) do.call("c",
lapply(allargs, "[[", i)))
以这种方式base
R
使用c
不保留“有序”属性,它甚至不返回一个因子!
例如(在base
R
)
f <- factor(1:2, levels = 2:1, ordered=TRUE)
g <- factor(1:2, levels = 2:1, ordered=TRUE)
# it isn't ordered!
is.ordered(c(f,g))
# [1] FALSE
# no suprise as it isn't even a factor!
is.factor(c(f,g))
# [1] FALSE
但是data.table
有一个 S3 方法c.factor
,用于确保返回一个因子并保留级别。不幸的是,此方法不保留有序属性。
getAnywhere('c.factor')
# A single object matching ‘c.factor’ was found
# It was found in the following places
# namespace:data.table
# with value
#
# function (...)
# {
# args <- list(...)
# for (i in seq_along(args)) if (!is.factor(args[[i]]))
# args[[i]] = as.factor(args[[i]])
# newlevels = unique(unlist(lapply(args, levels), recursive = TRUE,
# use.names = TRUE))
# ind <- fastorder(list(newlevels))
# newlevels <- newlevels[ind]
# nm <- names(unlist(args, recursive = TRUE, use.names = TRUE))
# ans = unlist(lapply(args, function(x) {
# m = match(levels(x), newlevels)
# m[as.integer(x)]
# }))
structure(ans, levels = newlevels, names = nm, class = "factor")
}
<bytecode: 0x073f7f70>
<environment: namespace:data.table
所以是的,这是一个错误。现在报告为#5019。