简体   繁体   中英

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

  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". I've tried changing things around but then I get other errors like 'x' must be numeric.

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.

If you want the correlation of temperature , bill , pizzas , and got_wine only against each other , then you should not pass pizza to cor(). Passing a data frame as the first argument (x) is enough to correlate its columns. If you also pass an argument y, it will correlate the columns from x with the columns of y.

I don't know why you try to use summarise(), but it won't work the way you're using it now.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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