The magrittr and base R pipe: what's the difference? (CC241)

Поделиться
HTML-код
  • Опубликовано: 21 авг 2024

Комментарии • 47

  • @Riffomonas
    @Riffomonas  Год назад

    Be sure to check out the follow up episode to this one!
    ruclips.net/video/TmSwDAvPX2Q/видео.html

  • @bassamsaleh8034
    @bassamsaleh8034 2 года назад +21

    great video, thanks a lot. just one side note starting with R version 4.2, you can use the (_) as data placeholder like the (.) in the Magrittr.
    so the syntax would be: |> cor.test(~prcp+ snow, data = _)

    • @Riffomonas
      @Riffomonas  2 года назад +2

      That’s great - thanks!

  • @CaptainChuck15
    @CaptainChuck15 2 года назад +4

    "ceci n'est pas une* pipe" is a thing of beauty. You should put it on a t-shirt!
    I'd buy it in a heartbeat.

    • @Riffomonas
      @Riffomonas  2 года назад +1

      🤓that’s the magic of the magrittr developers very cool tool

  • @sven9r
    @sven9r 2 года назад +2

    Finally Pat started the nerd talk! I love it!
    I want to add I never wrote a single time in my life the pipe manually.
    For the others besides Pat's highly trained fingers:
    Just press cntrl+shift + m on Windows or cmd + shift + m on my mac.

    • @Riffomonas
      @Riffomonas  2 года назад

      Lol - someday I’ll refrain my fingers 🤓

  • @AndreaDalseno
    @AndreaDalseno Год назад

    No wonder the output of line 10 is the same as line 7; they both use the same data 😁! Awesome video as usual.

  • @gimanibe
    @gimanibe 2 года назад +2

    Great video, Pat, Thanks a lot. I actually use the %T>% pipe inside functions quite often, for example, to plot and intermediate result or print a data.frame I do further work with.

    • @Riffomonas
      @Riffomonas  2 года назад

      Very cool. I’ll have to try using it more

  • @krytolandros1775
    @krytolandros1775 Год назад +2

    Very clear presentation. I want to add a few things to give a fairer comparison to base R (irrespective of the pipe). Base R has na.omit() and subset() which are essentially the same as drop_na() and filter(), so you can run the first set of code just as cleanly in base R as follows:
    local_weather |> na.omit() |> subset(snow > 0)
    Similarly, base R has the transform() and within() functions, which work very similar to mutate().
    As others have mentioned, we now have the _ placeholder for piping to arguments other than the first. Though it's also worth noting that |> pipes to the first *unnamed* argument. So no_na_no_zero |> cor.test(formula = ~prcp+snow) also works, because the first unnamed argument is data, which the dataset is then passed to. An alternative to %$% in base R is with(), e.g., no_na_no_zero |> with(cor.test(prcp, snow)). The first argument to with() is the dataset and the second is the expression you want to run, which makes it amenable to piping.
    Just wanted to add to the info you presented for those interested. I think base R gets a bad rap sometimes, but it's not always as obtuse as some make it out to be.

    • @Riffomonas
      @Riffomonas  Год назад

      Thanks for watching! I did a follow up episode where I covered much of this here:
      ruclips.net/video/IL31LG84i5M/видео.html

  • @trevorfowles4119
    @trevorfowles4119 2 года назад +1

    I cant believe i'm here. I can just watch R videos and understand things enough to not need to pause and go back to learn something, then resume.

    • @Riffomonas
      @Riffomonas  2 года назад

      Ha! Wonderful Trevor 🤓

  • @mabenba
    @mabenba 2 года назад +4

    The video we didn't know we needed came the exact same time we needed!! Thanks for sharing. I love your content!

    • @Riffomonas
      @Riffomonas  2 года назад +1

      Thanks for watching! 🤓

  •  2 года назад +2

    I didn't know there were so many pipes. Thanks, very interesting!

  • @rayflyers
    @rayflyers 2 года назад +3

    I'm a data scientist, and given the scale of data I work with, the difference in performance is a consideration for me. I won't deny that the dot notation for the standard Magrittr pipe is incredibly useful though. I'm pretty curious about the trade-off in performance between loading the magrittr pipe for base R functions like gsub versus using the Base R pipe for pipe-friendly functions from tidyverse packages like str_replace_all. I don't expect you to go into all that though. I can test it on my own. Also, huge thanks for the introduction to the other pipes and alias functions because I've been overlooking them, and they could come in super handy for more readable code. Great video!

    • @Riffomonas
      @Riffomonas  2 года назад +1

      Thanks! 🤓

    • @k1llyah
      @k1llyah 2 года назад +1

      if you are working on performance-critical applications, syntactic sugar like tidyverse could be an intermediate step to translate thoughts to code, then optimize afterward through dtplyr or translating to base R. Magrittr is part of the fastverse suit of packages, but has a minimal overhead (2 * substitute calls + environment juggling) whereas base R pipe relies on the parser, so transition between pipes should give a very minimal performance boost. A greater concern would be if your project needs such dependencies (and what happens if one dependency breaks)

    • @rayflyers
      @rayflyers 2 года назад

      @@k1llyah Wow, this is genuinely fantastic and thoughtful advice. Thank you so much!

  • @joydeepnag885
    @joydeepnag885 7 месяцев назад

    Thank you very much!!!! Super super informative.

  • @marc_CFA_applied_econ
    @marc_CFA_applied_econ 2 года назад +1

    Excellent video. Always forget that cor.test is available. Much easier to pipe in as opposed to the cumbersome dataframe %>% select(x,y) %>% cor(x, y). Also had no idea set_colnames existed. Was using a purr function for years. Similar, but one fewer argument which I always forgot. Thank you Dr. Pat.

  • @russtin1
    @russtin1 2 года назад +1

    I love the Ctrl-Shift-m hot key for the Magritter pipe. My fingers hate reaching for that top row

    • @Riffomonas
      @Riffomonas  2 года назад

      Some day I’ll learn it too 😂

  • @timmytesla9655
    @timmytesla9655 2 года назад +1

    Wow. I never knew there were other pipes other than %>%. Thanks for the video.

    • @Riffomonas
      @Riffomonas  2 года назад +1

      My pleasure! Thanks for watching Timmy🤓

  • @mikep8857
    @mikep8857 2 года назад +1

    I agree that the %T>% pipe is not terribly useful but I do like it to look at both the head and the tail of a data frame in one pipe.

    • @Riffomonas
      @Riffomonas  2 года назад

      Agreed. Thanks for watching!

  • @gavinmasterson2242
    @gavinmasterson2242 2 года назад +2

    Hi Pat. Thanks for the video. I haven't spent any time looking at the magrittr aliases and they do look useful for certain use cases.
    One comment though. When you mention show the base pipe workflow, you didn't use the `_` placeholder to replace the `.` from {magrittr}. I demonstrate the use of the placeholder below:
    ```
    data("CO2")
    CO2 |>
    cor.test(~ conc + uptake, data = _)
    ```
    The placeholder was added in R 4.2 (I think) and, as I understand it, the _ placeholder can only be used for a named argument - so not a positional argument, nor a ... argument.
    I still use the magrittr pipe by default for interactive workflows, but I think that the the R base pipe is a better fit when I work on packages.
    I also haven't found a use for the eager pipe and I also avoid the assignment pipe for the same reasons that you mention.
    If I want to produce a modified dataframe I will usually pipe the first assignment call:
    ```
    my_data %
    drop_na() %>%
    filter(....)
    ```
    This makes it obvious that my_data is not the raw data from the read_csv call, but doesn't give me eye-strain looking for %% hidden in my code.
    🤣

  • @musicspinner
    @musicspinner 2 года назад +2

    Tee Pipe is useful if you want to inject console progress messages while pipeline is executing.

    • @Riffomonas
      @Riffomonas  2 года назад

      Yup - I kinda mention this in the video

  • @jmr_262
    @jmr_262 2 года назад +2

    Fira Code font displays the native R pipe as an arrowhead which is super cool (but not the Magrittr pipe)

  • @elixirmarketing6961
    @elixirmarketing6961 2 года назад

    God damn, thank you

  • @musicspinner
    @musicspinner 2 года назад +3

    "Ceci n'est pas une pipe" ("This is not a pipe")
    - René Magritte
    hence `magrittr::%>%`

  • @rayflyers
    @rayflyers 2 года назад +4

    0:58 Look, mama, I'm famous!

    • @Riffomonas
      @Riffomonas  2 года назад +1

      Ha! Thanks for the question 🤓

    • @sven9r
      @sven9r 2 года назад

      you did what I never accomplished for years :D

  • @user-cg8ey9rv4u
    @user-cg8ey9rv4u Год назад +1

    just use %>% 😃

  • @dreznik
    @dreznik 10 месяцев назад

    you do know is that to direct the output of |> to an arbitrary arg w a function:
    ... |> \(x) cor.test(..., x)

  • @djangoworldwide7925
    @djangoworldwide7925 2 года назад +1

    I fell like in the base demonstration you could just as simple as the tidy way to write no_na_no_zero 0]) and it would be a lot simpler than the original code... Still of course its the tidy way or no way, but base R can also be readble

    • @Riffomonas
      @Riffomonas  2 года назад

      Thanks - again this is just meant to be one example of nesting base r functions. I think what you have written is a lot less readable than the piped version. The argument over pipes or not is really about readability.