Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R4DS error comparison (1) is possible only for atomic and list types

Tags:

r

filter

ggplot2

In R4DS Section 3.6, the authors present the following code:

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(color = class)) + 
  geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)

which causes the following error

Error in class == "subcompact" : 
  comparison (1) is possible only for atomic and list types

I assume it worked when the authors wrote it, as they have a nice plot illustrating the results.

What is happening and how do I fix it? (R 3.3.2 on OS X) Thanks

like image 312
Bill Raynor Avatar asked Jan 26 '17 03:01

Bill Raynor


3 Answers

The filter() function comes from the dplyr package. Be sure you've loaded it before running those lines. Otherwise, you're running a comparison with class(), the built-in function, rather than mpg$class.

like image 157
coletl Avatar answered Oct 19 '22 10:10

coletl


You probably had another package with a function (filter) loaded and masking dplyr filter

quick and dirty fix:

dplyr::filter()

instead of

filter()
like image 38
mzakaria Avatar answered Oct 19 '22 09:10

mzakaria


use library(dplyr)

library(dplyr)

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
  geom_point(mapping = aes(color = class)) +
  geom_smooth(
    data = filter(mpg, class == "subcompact"),
    se = FALSE)
like image 1
user10730916 Avatar answered Oct 19 '22 10:10

user10730916