How to use the %% operator in R

6
  • What does the %<>% operator in R mean?
  • What's your difference from <- ?
  • Under what circumstances can it be useful?
  • asked by rafa.pereira 12.06.2016 в 11:48

    1 answer

    7

    This operator is from the magrittr package and it is for you to pass an object to a function at the same time by 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 to numeric. One way to do this is to assign x to the result of the as.numeric function on itself:

    x <- as.numeric(x)
    

    With %<>% you do the same thing without having to type x twice, because the %<>% operator passes x to as.numeric and then rewrites x :

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

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

    In the background this is a syntax question, such as %>% .

        
    12.06.2016 / 15:39