我想解決stats::reorder vs Hmisc::reorder的奇怪重疊。 沒有Hmisc加載我得到我想要的結果,即無序因素: > with(InsectSprays, reorder(spray, count, median))
[1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D
我很驚訝地看到R在連接向量時會將因素強制爲一個數字。即使級別相同,也會發生這種情況。例如: > facs <- as.factor(c("i", "want", "to", "be", "a", "factor", "not", "an", "integer"))
> facs
[1] i want to be a factor not an integer
Levels: a
定義 x <- data.frame(
ID=letters[1:10],
class = as.factor(c(rep(1,5),rep(2,5))),
treat = rep(0,10))
S.T. > x
ID class treat
1 a 1 0
2 b 1 0
3 c 1 0
4 d 1 0
5 e 1 0
6