simplePipesBasic pipes are used for chaining (rather than nesting) commands. They assume that the piped values are the first and only arguments passed to the next command.
First, name a variable and assign an array of values back to it.
variable <- c(1, 2, 6, 8, 9, 15)
Though it’s not commonly demonstrated, R does permit forward assignment
of vales to a variable.
c(1, 2, 6, 8, 9, 15) -> variable
In standard R syntax, get the mean of the variable.
mean(variable)
## [1] 6.833333
After version 4.1, R includes a native forward pipe. Here, variable is
piped directly to get its mean.
variable |> mean()
## [1] 6.833333
Using the forward basic pipe, identify the variable and then get its
mean.
variable %>% mean
## [1] 6.833333
Using the backward basic pipe, get the mean of the variable.
mean %<% variable
## [1] 6.833333