Created
January 28, 2018 01:05
-
-
Save stephhazlitt/c6ccc36b5ea0ff190cf479659d164b8e to your computer and use it in GitHub Desktop.
reprex for getting help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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: