这是我的数据
> a
[1] Male Male Female Male Male Male Female Female Male Male Female Male Male Male
[15] Female Female Female Male Female Male Female Male Male Female Male Male Female Male
[29] Male Male Female Male Male Male Female Female Male Male Male Male Male
Levels: Female Male
> b
[1] 0 1 0 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 1 0 1 1 1 1
Levels: 0 1
> table(a,b)
b
a 0 1
0 0
Female 10 4
Male 12 15
我不知道为什么table(a,b)
的结果有一行(0 0)
,我的预期结果如下:
> table(a,b)
b
a 0 1
Female 10 4
Male 12 15
你能告诉我为什么会这样,以及如何纠正它,谢谢!
> dput(a)
structure(c(3L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L,
3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L,
3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("",
"Female", "Male"), class = "factor")
> dput(b)
structure(c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("0",
"1"), class = "factor")
答案 0 :(得分:1)
来自上述评论:
这种情况正在发生,因为a
中有一个空因子级别:
> levels(a)
[1] "" "Female" "Male"
你可以继续制作一张无法考虑空因子水平的表格(来自@ lmo的评论):
table(as.character(a), b)
或者,您可以轻松删除因素级别而无需观察(来自@ Dave2e的评论)
a <- droplevels(a) table(a, b)