gpt4 book ai didi

R t.test() 以 data.frames 作为参数

转载 作者:行者123 更新时间:2023-12-02 09:21:37 25 4
gpt4 key购买 nike

CrossValidated 上有一个问题,有人向 t.test 提供了两个数据帧而不是两个向量。功能:https://stats.stackexchange.com/questions/261830/t-test-or-wilcox-in-r-and-how-to-apply-to-dataframe-splitted-in-2-groups/

请参阅此代码以获取更简短的示例

a <- data.frame(foo=1:5, bar=5:9)
b <- data.frame(foo=1:5, bar=5:9)
t.test(a,b)

help t.test页面函数明确指出 x 和 y 应该是

a (non-empty) numeric vector of data values.

上面的代码仍然没有抛出任何错误,但给出了结果。结果有什么意义?

最佳答案

你可以看一下里面的代码:

 stats:::t.test.default

我在这里展示了一些选定的代码

function (x, y = NULL, alternative = c("two.sided", "less", "greater"), 
mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95,
...)
{
alternative <- match.arg(alternative)
if (!missing(mu) && (length(mu) != 1 || is.na(mu)))
### snip
if (!is.null(y)) {
### snip
yok <- !is.na(y)
xok <- !is.na(x)
### snip
y <- y[yok]

所以我们确实有一个 y 参数,您将看到 yok 将导致一个选择,当在 y[yok] 中使用时,该选择将转换为向量。最后,一切都将发生在强制转换为向量的 data.frame 上(as.vector)。

绝对不是人们想要做的事情,而且也违反了用户的规范......

关于R t.test() 以 data.frames 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225298/

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