Imported Data not working in R

I created a dataset in excel and then I saved it to my computer as a .csv and was able to import it into R. I can use the view() function, but whenever I try to manipulate the dataset, I get the message:
Error: Problem with mutate() input Total_wins.
:heavy_multiplication_x: object 'Home_win' not found

And whenever I try to use it with ggplot, I get the following message:
Error in[[i]], optional = TRUE) :
cannot coerce class ‘c("LayerInstance", "Layer", "ggproto", "gg")’ to a data.frame

What can I do to make my dataset useable in R?

Please show the code you use to read in the data and your attempt to use the mutate() function. A sample of the data would also be useful. You can produce that with


where I assume Home_win is the name of the data frame. You can format the pasted output nicely by putting a line with three back ticks, ```, just before and just after that pasted content, like this
output of dput here

This is the code I used to read it in:
pens_and_wins <- read_csv("pens and wins.csv")

This is the code I used for mutate:
pens_and_wins <- pens_and_wins %>% mutate (Total_wins = Home_win+Away_win)

  Team_year       Home_Win Away_Win Home_pens Away_pens
  <chr>              <dbl>    <dbl>     <int>     <int>
1 Buffalo2009            3        3        63        57
2 Miami2009              4        3        49        45
3 New England2009        8        2        62        70
4 N.Y. Jets2009          4        5        62        59
5 Pittsburgh2009         6        3        49        56
6 Baltimore2009          6        3        57        57

Thank you!

R is case sensitive. Your column is named Home_Win but in the mutate call you use Home_win. The Away_Win column has the same problem.

Yeah I realized that right after I sent...
I'm still getting an error message when I try to run a ggplot though.

chart4 <- ggplot (data=pens_and_wins) +
  geom_point(mapping = aes (x=Home_pens, y = Away_pens))
view (chart4)

Error in[[i]], optional = TRUE) :
cannot coerce class ‘c("LayerInstance", "Layer", "ggproto", "gg")’ to a data.frame

I do not get an error when I try to View a ggplot object but I suspect you do not want to view its internal structure, you want to see the plot. Try


If you still get an errorm try running this code.

DF <- data.frame(x = 1:4,y = 1:4)

PLT <- ggplot(DF)  + geom_point(mapping = aes(x = x, y = y))

That worked! thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.