How to use %<> % operator in R

Asked

Viewed 392 times

7

  1. What the operator means %<>% in r ?
  2. What is your difference from <- ?
  3. Under what circumstances can it be useful?

1 answer

7


That operator belongs to the package magrittr and it serves for you to pass an object to a function while modifying the object you passed.

For example, suppose the following x in text format:

library(magrittr)    
x <- "1"

Suppose you want to convert this x for numeric. One way to do this is to assign the x the result of the function as.numeric in itself:

x <- as.numeric(x)

With the %<>% you do the same thing without having to write the x twice as the operator %<>% passes the x to the as.numeric and then rewrite x:

x %<>% as.numeric # mesma coisa de x <- x %>% as.numeric

That is, while the <- only modifies the left-hand variable with the right-hand expression, the %<>% modifies the left variable after passing this variable to a function on the right.

At the bottom this is a matter of syntax, such as the %>%.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.