PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

[FIXED] Why will this R pipe run but not this one?

 December 14, 2022     dplyr, r, syntax     No comments   

Issue

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 " %>%"


Solution

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.



Answered By - Gregor Thomas
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing