0

I am trying to get NSE values grouped by a variable. I tried something similar to:

library(dplyr)
library(hydroGOF)

mtcars %>% group_by(cyl) %>% 
  NSE(wt,drat)

Why isn't it working? It doesn't find "wt". Thank you.

Matt_4
  • 147
  • 1
  • 12

1 Answers1

2

Here's an approach with summarise:

mtcars %>% 
  group_by(cyl) %>%
  summarise(NSE = NSE(wt, drat))
# A tibble: 3 x 2
    cyl    NSE
  <dbl>  <dbl>
1     4 -30.2 
2     6  -2.22
3     8 -10.2 

The reason yours wasn't working is because %>% redirects the output of the previous function into the first argument of the next. So yours was the equivalent of NSE(mtcars,wt,drat). And since wt isn't defined in the global environment, it wasn't found.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57