Fecha de la ultima revisión

## [1] "2021-02-01"

El tema proviene de los siguientes sitios.

English: https://r4ds.had.co.nz/pipes.html

Español: https://es.r4ds.hadley.nz/pipes.html


Temas: El paquete “magrittr”

  • %>% pipes

Alternativas a los pipes

  • Pasos intermedios
  • Sobre escribir el original

Composición de funciones


Uso de pipe


Cuándo no usar el pipe


Otras herramientas de magrittr

  • %T>%
  • %$%
library(magrittr)

iris %>%
  subset(Sepal.Length > mean(Sepal.Length)) %$%
  cor(Sepal.Length, Sepal.Width)
## [1] 0.3361992
  • %<>%
# Example 1
head(iris)
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93  1.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5  3.61.40.2setosa
5.43.91.70.4setosa
head(iris$Sepal.Length %<>% sqrt)
## [1] 2.258318 2.213594 2.167948 2.144761 2.236068 2.323790
# Example 2
x <- rnorm(100)
head(x, n=10)
##  [1] -0.57300818 -2.66290342  0.43319709  0.45699657  1.03790838  0.09755769
##  [7] -0.36395359  1.79147565  0.33230936 -0.36733552
x %<>% 
  abs %>% 
  sort

head(x, 20)
##  [1] 0.03627992 0.07089837 0.09755769 0.13585312 0.14371857 0.14776025
##  [7] 0.16107989 0.16646851 0.17321555 0.18242025 0.18251129 0.19571995
## [13] 0.19948039 0.20325103 0.22255672 0.25648246 0.30316201 0.33041543
## [19] 0.33230936 0.33880104