Skip to content

Instantly share code, notes, and snippets.

@stephhazlitt
Created January 28, 2018 01:05
Show Gist options
  • Save stephhazlitt/c6ccc36b5ea0ff190cf479659d164b8e to your computer and use it in GitHub Desktop.
Save stephhazlitt/c6ccc36b5ea0ff190cf479659d164b8e to your computer and use it in GitHub Desktop.
reprex for getting help
library(dplyr) ## starwars data ships with dplyr
library(tibble)
## I am trying to use add_row() to add a total row with a sum of the other rows.
## This does not work
foo <- starwars %>%
select(name, height) %>%
filter(height > 200) %>%
add_row(name = "Total", height = sum(height))
## This works - so clearly something incorrect with using the sum function in add_row()?
foo <- starwars %>%
select(name, height) %>%
filter(height > 200) %>%
add_row(name = "Total", height = 300)
@stephhazlitt
Copy link
Author

Answer and tutorial from @boshek:

When you are working in a pipe you typically don’t have to explicitly reference the data that are you using (i.e. with $) so you can just reference columns by their unquoted name. However when you add an additional layer of abstraction like height = sum(height) R gets confused and doesn’t know where to look. You help it by adding “.$” which tells R to look for height in the same data that you are currently working with in your add_row line. Think about it as if you were splitting up your pipe into pieces:

## First operation is to get all the data where height is greater than 200:
foo <- starwars %>%
  select(name, height) %>%
  filter(height > 200)
 
## Then separate out `add_row` to illustrate where the data is coming from:
add_row(foo, name = "Total", height = sum(foo$height))

However since we want to operate within a pipe we can drop the first data argument (because %>% automatically passes the object created by the previous line to the current lines first argument) and replace any other instances by the . which results in grabbing the same data that was passed with the pipe. Combining it all together:

foo <- starwars %>%
  select(name, height) %>%
  filter(height > 200) %>%
  add_row(name = "Total", height = sum(.$height))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment