简体   繁体   English

RStudio相关矩阵,Cor()'y'中的错误必须是数字

[英]RStudio Correlation Matrix, Error in Cor ()'y' must be numeric

I'm brand new to coding.我对编码是全新的。 I'm trying to create a correlation matrix for temperature, bill, pizzas, and got_wine and assign it to Q1我正在尝试为温度、账单、披萨和 got_wine 创建一个相关矩阵并将其分配给 Q1

  Q1 <- pizza %>%
  select (temperature, bill, pizzas, got_wine) %>%
  summarise(na.rm=TRUE) %>%
  cor(pizza, use="complete.obs")

This is the code I've created so far.这是我到目前为止创建的代码。 But no matter what I change I get the error "Error in cor(., pizza, use = "complete.obs") : 'y' must be numeric".但无论我更改什么,我都会收到错误“cor(., Pizza, use = "complete.obs") 中的错误:'y' must be numeric”。 I've tried changing things around but then I get other errors like 'x' must be numeric.我试过改变周围的东西,但后来我得到了其他错误,比如“x”必须是数字。

I've got another problem, very similar.我还有一个问题,非常相似。 I'm trying to create a correlation matrix of the relationships between time, temperature, bill, and pizzas for Laura in the East branch.我正在尝试为东分店的劳拉创建时间、温度、账单和比萨饼之间关系的相关矩阵。 So far I've got:到目前为止,我有:

Q2 <- pizza %>% 
  filter (operator == 'Laura' & branch == 'East') %>%
  select (time, temperature, bill, pizzas) %>%
  cor (pizza, use="complete.obs")

Is there something obvious you can tell me?有什么明显的可以告诉我吗? This is supposed to be fairly basic code that only uses tidyverse and lm.beta.这应该是相当基本的代码,只使用 tidyverse 和 lm.beta。

If you want the correlation of temperature , bill , pizzas , and got_wine only against each other , then you should not pass pizza to cor().如果您只希望temperaturebillpizzasgot_wine相互关联,那么您应该将pizza传递给 cor()。 Passing a data frame as the first argument (x) is enough to correlate its columns.将数据框作为第一个参数 (x) 传递就足以关联其列。 If you also pass an argument y, it will correlate the columns from x with the columns of y.如果您还传递了一个参数 y,它会将 x 的列与 y 的列相关联。

I don't know why you try to use summarise(), but it won't work the way you're using it now.我不知道你为什么尝试使用 summarise(),但它不会像你现在使用的那样工作。

What you are probably looking for is this:您可能正在寻找的是:

Q1 <- pizza %>%
select (temperature, bill, pizzas, got_wine) %>%
cor(use="complete.obs")
Understanding the pipe (%>%)了解管道 (%>%)

I would recommend to lay off the pipe, or at least use it sparingly, until you have a bit more experience in R. While it does improve code legibility, the downside is that you won't be able to look at the intermediary outcomes.我建议放弃管道,或者至少谨慎使用它,直到您对 R 有更多的经验。虽然它确实提高了代码的易读性,但缺点是您将无法查看中间结果。

What %>% does is pass the outcome of the code before the pipe, as the first argument to the command after the pipe. %>%所做的是在管道之前传递代码的结果,作为管道之后命令的第一个参数。 Example:例子:

pizza %>%
select(temperature, bill, pizzas, got_wine) %>%
cor(pizza, use="complete.obs")

is equivalent to:相当于:

selection <- select(pizza, temperature, bill, pizzas, got_wine)
cor(selection, pizza, use="complete.obs")

In the latter example, the order of arguments corresponds with the documentation, and you can easily check the contents of selection to see if they contain what you intended them to contain.在后一个示例中,参数的顺序与文档相对应,您可以轻松检查selection的内容以查看它们是否包含您希望它们包含的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM