We began by loading the 'palmerpenguins' data set from the tidyverse package.
library(tidyverse)
library(palmerpenguins)
This is the code that will run:
penguins %>% group_by(island) %>% drop_na() %>% summarise(mean_bill_length_mm = mean(bill_length_mm))
Why will this same code but with tabs and indention not run?
penguins %>% group_by(island)
%>% drop_na()
%>% summarise(mean_bill_length_mm = mean(bill_length_mm))
When I run this code I get the following error:
Error: unexpected SPECIAL in " %>%"
CodePudding user response:
You've probably seen code from other programming languages where every line ends with a semicolon ;
. R doesn't require that (though you actually can use semicolons to indicate line endings in R).
Instead, the R interpreter is always looking for a valid line-ending when a command looks "complete", and then it assumes the command has ended, it executes it, and expects the next line to start a new command.
So in R, we can do this:
## this works
1 * 2 *
3
# [1] 6
R knows to expect more code after a *
. You can't end a valid statement with *
, so, even though there's a line break, R is expecting the command to continue.
But this is an error:
1 * 2
* 3
# Error: unexpected '*' in " *"
1 * 2
is perfectly valid. R has no reason to expect the command to continue, so 1 * 2
is run, and then R expects the next line to be a new command. But * 3
isn't valid syntax, so there is an error.
Your pipe is just the same:
penguins %>% group_by(island)
## this looks like a complete command!
%>% drop_na()
## this looks like a new line starting with %>%
## (which is an Error, because you need something before a pipe)
# Error: unexpected SPECIAL in " %>%"
So, when using pipes, you need to use the pipe at the end of each line to tell R that that line is going to continue.