0

I've got this one-liner:

aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean)

Now I want to filter by mpg>25 like his:

aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean)[mpg>25,]

But now I get some 1743 lines of NA entries

any ideas?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
  • 1
    What object is `mpg`? I get "> aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean)[mpg>25,] Error in `[.data.frame`(aggregate(. ~ gear, mtcars[mtcars$mpg > 20, c("gear", : object 'mpg' not found" . Do you have anywhere `attach()` in your code? – jogo Feb 27 '16 at 18:50

3 Answers3

2

A dplyr solution could be:

 mtcars %>% group_by(gear) %>% filter(mpg>25) %>% summarise(mean(mpg))

or rather:

mtcars %>% group_by(gear) %>% filter(mpg>20) %>% 
summarise(mean(mpg)) %>% filter(`mean(mpg)`>25)
count
  • 1,328
  • 9
  • 16
2

You can't (and I don't get lots of NAs, I correctly get an error because mpg isn't found. Did you do something previously that allows mpg to be found?). R doesn't work the way you are trying to force it.

You will need

tmp <- aggregate(. ~ gear,
                 data = subset(mtcars, mpg > 20, select = c('gear','mpg')),
                 FUN = mean)
with(tmp, tmp[mpg > 25, ])

> with(tmp, tmp[mpg > 25, ])
  gear   mpg
2    4 25.74
3    5 28.20
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
2

Here is another solution with base functions:

subset(aggregate(. ~ gear,mtcars[mtcars$mpg>20,c('gear','mpg')],mean), mpg>25)

and here is a solution with data.table

library(data.table)
M <- data.table(mtcars)
M[mpg>20, .(mpg=mean(mpg)), by=gear][mpg>25]
jogo
  • 12,469
  • 11
  • 37
  • 42