Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does %T>% function mean in R?

Tags:

r

can you please explain this pipe - %T>% with some examples if possible. I found only one example with the plot:

rnorm(200) %>%
matrix(ncol = 2) %T>%
plot %>% # plot usually does not return anything.
colSums

https://www.rdocumentation.org/packages/magrittr/versions/1.5/topics/%25T%3E%25

But from this and description I don't understand the usage. Thank you

like image 829
Retko Avatar asked Mar 19 '26 14:03

Retko


2 Answers

Both x %>% f() and x %T>% f() run f(x) but the difference is that the first one returns the output of f(x) whereas the second one returns x.

1) plot. %T>% is typicaly used with plot or other commands that don't return anything. Such commands are run for their side effects, in this case plotting, rather than their return value. Since plot only returns NULL it is also the case that BOD %>% plot only returns NULL so if we want to keep going in the pipeline we can't. If instead of %>% we use %T>% then we still can.

library(dplyr)
library(magrittr)

BOD %T>%
  plot %>%
  mutate(demand = demand + 1)

2) str. Another example is if we want to debug a pipeline by looking at an intermediate result. str does not return anything so if we want to continue in the pipeline we can use %T>% .

library(dplyr)
library(magrittr)

BOD %T>%
  str %>%
  mutate(demand = demand + 1)

3) lm/summary Suppose we want to display the output from summary in a pipeline but then go on to calculate the residual sum of squares, i.e. deviance. We want deviance to be applied to the output of lm, not the output of print(summary(.)). Note: when piping into a bracketed expression that . must be explicitly used, as seen in summary(.).

library(dplyr)
library(magrittr)

BOD %>%
  lm(demand ~ Time, data = .) %T>%
  { summary(.) %>% print } %>%
  deviance

4) lm Suppose we want to run lm on rows 1:4, 2:5 and 3:6 separately. Then we can use %T>% multiple times like this:

library(magrittr)

BOD %T>%
  { lm(demand ~ Time, data = ., subset = 1:4) %>% print } %T>%
  { lm(demand ~ Time, data = ., subset = 2:5) %>% print } %>%
  { lm(demand ~ Time, data = ., subset = 3:6) %>% print }

This example does illustrate multiple uses of %T>% in the same pipeline; however, it could be done even more easily without using any pipelines by making use of update.

fm <- lm(demand ~ Time, data = BOD)
update(fm, subset = 1:4)
update(fm, subset = 2:5)
update(fm, subset = 3:6)

Alternatives

It is possible to get the same effect without %T>% in other ways. Using the first example, this runs plot explicitly returning the input dot.

library(dplyr)

BOD %>%
  { plot(.); . } %>%
  mutate(demand = demand + 1)

A second alternative is to break it into two pipelines:

library(dplyr)

BOD %>% plot
BOD %>% mutate(demand = demand + 1)

A third alternative is to define a function which returns its input:

library(dplyr)

plot_ <- function(data, ...) { plot(data, ...); data }
BOD %>%
  plot_ %>%
  mutate(demand = demand + 1)

Similar alternatives could apply to the other examples too.

like image 157
G. Grothendieck Avatar answered Mar 21 '26 04:03

G. Grothendieck


library(magrittr)

Pipe operator %>% returns RHS value

c(1, 2) %>% sum
#[1] 3

c(1, 2) %>% sum %>% sqrt
#[1] 1.73205

whereas %T>% returns the original LHS value

c(1, 2) %T>% sum
#[1] 1 2

c(1, 2) %T>% sum %T>% sqrt
#[1] 1 2
like image 41
Ronak Shah Avatar answered Mar 21 '26 03:03

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!