gpt4 book ai didi

r - 包含 R > 3.4.0 中的显式 NA 的表

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

编辑:接受的答案帮助我的眼睛掉下来了;这种变化是一种改进,毕竟并不烦人。

table 的帮助文件中,现在写成:

Non-factor arguments a are coerced via factor(a, exclude=exclude). Since R 3.4.0, care is taken not to count the excluded values (where they were included in the NA count, previously).



这很烦人。之前,您可以调用 table(x, exclude = NULL)并明确确认 NA 的数量值(value)观。现在,如果没有,你不会被告知。观察:
vec_with_no_nas <- c("A", "B", "B", "C")
vec_with_nas <- c("A", "B", NA, "C")

table(vec_with_no_nas)
table(vec_with_no_nas, exclude = NULL)

table(vec_with_nas)
table(vec_with_nas, exclude = NULL)

这给出了输出:
> table(vec_with_no_nas)
vec_with_no_nas
A B C
1 2 1
> table(vec_with_no_nas, exclude = NULL)
vec_with_no_nas
A B C
1 2 1

看?没有明确确认零 NA。

我真正想要的是类似于旧行为的东西,即:
> table(vec_with_no_nas, exclude = NULL)
vec_with_no_nas
A B C <NA>
1 2 1 0

FWIW,如果向量确实有 NA 值, table(x, exclude = NULL)会告诉你:
> table(vec_with_nas)
vec_with_nas
A B C
1 1 1

> table(vec_with_nas, exclude = NULL)
vec_with_nas
A B C <NA>
1 1 1 1

我在基地和 tidyverse 工作.有上门服务 table将明确确认没有 NA 的替代品?

最佳答案

您可以尝试将 useNA 参数设置为“始终”。在 R 3.2.5 中,

table(vec_with_no_nas, useNA="always")

添加一个 NA 列,即使不存在 NA。
vec_with_no_nas
A B C <NA>
1 2 1 0

3.4.0(和 3.2.5)的在线帮助文​​件说

useNA controls if the table includes counts of NA values.



所以这个论点似乎直接解决了你想做的事情。 exclude 参数允许用户直接从表输出中删除因子变量的级别。
table(vec_with_no_nas, exclude="A")
vec_with_no_nas
B C
2 1

这比从构造的表对象中删除不需要的级别更干净。

笔记:
在线 3.4.0 帮助文件提到了同时使用 exclude 和 useNA 参数的病态案例,还提供了一个可能值得进一步探索的示例。

关于r - 包含 R > 3.4.0 中的显式 NA 的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803004/

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