data.table在rbind,R之后丢失因子排序

时间:2013-10-25 00:42:39

标签: r sorting data.table

rbind两个data.table有序因子时,排序似乎丢失了:

dtb1 = data.table(id = factor(c("a", "b"), levels = c("a", "c", "b"), ordered=T), key="id")
dtb2 = data.table(id = factor(c("c"), levels = c("a", "c", "b"), ordered=T), key="id") 
test = rbind(dtb1, dtb2)
is.ordered(test$id)
#[1] FALSE

有什么想法或想法吗?

3 个答案:

答案 0 :(得分:7)

data.table做了一些花哨的步法,这意味着在对象data.table:::.rbind.data.table上调用rbind时会调用data.tables.rbind.data.table利用与rbindlist相关联的加速比,并通过名称等进行额外检查。

.rbind.data.table使用c组合它们来处理因子列(因此保留了level属性)

# 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

答案 1 :(得分:1)

version 1.8.11 data.table开始,如果存在全局订单,则会将有序因素合并为ordered,如果不存在则会抱怨并产生一个因素:

DT1 = data.table(ordered('a', levels = c('a','b','c')))
DT2 = data.table(ordered('a', levels = c('a','d','b')))

rbind(DT1, DT2)$V1
#[1] a a
#Levels: a < d < b < c

DT3 = data.table(ordered('a', levels = c('b','a','c')))
rbind(DT1, DT3)$V1
#[1] a a
#Levels: a b c
#Warning message:
#In rbindlist(lapply(seq_along(allargs), function(x) { :
#  ordered factor levels cannot be combined, going to convert to simple factor instead

相比之下,这就是基地R的作用:

rbind(data.frame(DT1), data.frame(DT2))$V1
#[1] a a
#Levels: a < b < c < d
# Notice that the resulting order does not respect the suborder for DT2

rbind(data.frame(DT1), data.frame(DT3))$V1
#[1] a a
#Levels: a < b < c
# Again, suborders are not respected and new order is created

答案 2 :(得分:-1)

我在rbind之后遇到了同样的问题,只是为列重新分配了有序的级别。

test$id <- factor(test$id, levels = letters, ordered = T)

rbind

之后定义因子会更好