gpt4 book ai didi

r - 计算 R 中的频率

转载 作者:行者123 更新时间:2023-12-04 11:33:41 25 4
gpt4 key购买 nike

这是我的数据

> 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")

最佳答案

从上面的评论:

发生这种情况是因为 a 中有一个空因子水平:

> levels(a)
[1] "" "Female" "Male"

您可以继续生成一个忽略空因子水平的表(来自@lmo 的评论):

table(as.character(a), b)


或者,您可以在没有观察的情况下轻松删除因子水平(来自@Dave2e 的评论)

a <- droplevels(a)
table(a, b)

关于r - 计算 R 中的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38214114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com