DS4PS / course_website

https://ds4ps.github.io/course_website/
0 stars 0 forks source link

Cannot access the data for Lab 6 #4

Open BissKuttner opened 6 years ago

BissKuttner commented 6 years ago

I wanted to start on Lab 6 by trying create a simple plot. I need help being able to get to the Lahman data. I believe I loaded the data because I was able to take a peek at it. However when I try the simple plot, I get an error stating my variable for the year (yearID) is not found. What am I missing? Here is what my coding looks like with the error message at the end:

library( dplyr )
library( pander )
library( Lahman )
data( Teams )
# head( dat )
head( Teams )
ave.so <- Teams$SO / Teams$G
plot(
  x=yearID, y=ave.so,
  xlab="years",
  ylab="SO",
  main="Strikeouts per game per team (by batter)"
)

Error in plot(x = yearID, y = ave.so, xlab = "years", ylab = "SO", main = "Strikeouts per game per team (by batter)") : object 'yearID' not found

lecy commented 6 years ago

This is a great question. Note that the plot() function is looking for two vectors, so you need to reference variables using the $ operator:

library( Lahman )
data( Teams )
plot( x=Teams$SO, y=Teams$G )
ave.so <- Teams$SO / Teams$G

When you create the new variable "ave.so", you can either reference one vector in the data frame, and another by object name:

ave.so <- Teams$SO / Teams$G
plot( x=Teams$yearID, y=ave.so )

Or you can add the variable back to the data frame, and reference both in the data frame:

Teams$ave.so <- Teams$SO / Teams$G

# OR

library( dplyr )
Teams <- mutate( Teams, ave.so=SO/G )

plot( x=Teams$yearID, y=Teams$ave.so )