【发布时间】:2014-09-25 22:49:31
【问题描述】:
我有一个整数向量,我希望我可以将其视为数字向量:
> class(pf$age)
[1] "integer"
> is.numeric(pf$age)
[1] TRUE
但是,当我尝试使用它来计算相关性时,出现错误:
> cor.test(x = "age", y = "friend_count", data = pf)
Error in cor.test.default(x = "age", y = "friend_count", data = pf) :
'x' must be a numeric vector
我对替代语法的最佳猜测也不起作用:http://pastie.org/9595290
发生了什么事?
编辑:
以下语法有效:
> x = pf$age
> y = pf$friend_count
> cor.test(x, y, data = pf, method="pearson", alternative="greater")
但是,我不明白为什么我不能在函数中指定 x 和 y(你可以使用其他 R 函数,如 ggplot)。 ggplot和cor.test有什么区别?
【问题讨论】:
-
@Gregor,它没有。我将该示例包含在我的备用语法中。
-
糟糕,我的错误是我在全局环境中定义了
age和friend_count,而我的data参数被忽略了。 -
@Gregor 别担心 :-)