0

这是一个可能的错误factor还是我在这里遗漏了什么?

xx <- seq.Date(Sys.Date(), length.out=5, by="1 day")

factor(xx) # Works fine!
[1] 2013-07-12 2013-07-13 2013-07-14 2013-07-15 2013-07-16
Levels: 2013-07-12 2013-07-13 2013-07-14 2013-07-15 2013-07-16

factor(xx, levels=unique(xx)) # Generates NAs.
[1] <NA> <NA> <NA> <NA> <NA>
Levels: 2013-07-12 2013-07-13 2013-07-14 2013-07-15 2013-07-16
4

1 回答 1

2

这是因为因子水平是characters,xx而是Date。将向量传递character给 levels 参数按预期工作:

factor(xx, levels=as.character(unique(xx)))
[1] 2013-07-12 2013-07-13 2013-07-14 2013-07-15 2013-07-16
Levels: 2013-07-12 2013-07-13 2013-07-14 2013-07-15 2013-07-16
于 2013-07-12T12:07:48.790 回答