Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 256
7.3.4.2 The T-Pipe
ОглавлениеThis works nice, but now imagine that we want to keep “t” as the tibble, but still add some operations on it – for example plot it. In that case, there is the special %T>%
“T-pipe” that will rather pass on the left side of the expression than the right side. The output of the code below is the plot in Figure 7.3 on page 136.
Figure 7.3: A linear model fit on generated data to illustrate the piping command.
library(magrittr) t <- tibble(“x” = runif(100)) %>% within(y <- 2 * x + 4 + rnorm(10, mean=0, sd=0.5)) %T>% plot(col=“red”) # The function plot does not return anything # so we used the %T>% pipe. lm3 <- t %$% lm(y ~ x) %T>% # pass on the linear model summary %T>% # further pass on the linear model coefficients tcoef <- lm3 %>% coefficients # we anyhow need the coefficients # Add the model (the solid line) to the previous plot: abline(a = tcoef[1], b=tcoef[2], col=“blue”, lwd=3)