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

  Рет қаралды 5,070

Riffomonas Project

Riffomonas Project

Күн бұрын

Пікірлер: 47
@Riffomonas
@Riffomonas Жыл бұрын
Be sure to check out the follow up episode to this one! kzbin.info/www/bejne/ip62qHd3q7WLaLM
@bassamsaleh8034
@bassamsaleh8034 2 жыл бұрын
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 жыл бұрын
That’s great - thanks!
@CaptainChuck15
@CaptainChuck15 2 жыл бұрын
"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 жыл бұрын
🤓that’s the magic of the magrittr developers very cool tool
@krytolandros1775
@krytolandros1775 Жыл бұрын
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: kzbin.info/www/bejne/f32WYn99bZmca68
@mabenba
@mabenba 2 жыл бұрын
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 жыл бұрын
Thanks for watching! 🤓
@gimanibe
@gimanibe 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
@sven9r
@sven9r 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 🤓
@trevorfowles4119
@trevorfowles4119 2 жыл бұрын
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 🤓
2 жыл бұрын
I didn't know there were so many pipes. Thanks, very interesting!
@joydeepnag885
@joydeepnag885 7 ай бұрын
Thank you very much!!!! Super super informative.
@rayflyers
@rayflyers 2 жыл бұрын
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 жыл бұрын
Thanks! 🤓
@k1llyah
@k1llyah 2 жыл бұрын
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!
@marc_CFA_applied_econ
@marc_CFA_applied_econ 2 жыл бұрын
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.
@Riffomonas
@Riffomonas 2 жыл бұрын
Thanks Marc!
@russtin1
@russtin1 2 жыл бұрын
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 жыл бұрын
Wow. I never knew there were other pipes other than %>%. Thanks for the video.
@Riffomonas
@Riffomonas 2 жыл бұрын
My pleasure! Thanks for watching Timmy🤓
@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.
@mikep8857
@mikep8857 2 жыл бұрын
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!
@musicspinner
@musicspinner 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
@gavinmasterson2242
@gavinmasterson2242 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. 🤣
@Riffomonas
@Riffomonas 2 жыл бұрын
Thanks! 🤓
@jmr_262
@jmr_262 2 жыл бұрын
Fira Code font displays the native R pipe as an arrowhead which is super cool (but not the Magrittr pipe)
@Riffomonas
@Riffomonas 2 жыл бұрын
Thanks for the info!
@musicspinner
@musicspinner 2 жыл бұрын
"Ceci n'est pas une pipe" ("This is not a pipe") - René Magritte hence `magrittr::%>%`
@elixirmarketing6961
@elixirmarketing6961 2 жыл бұрын
God damn, thank you
@Riffomonas
@Riffomonas 2 жыл бұрын
My pleasure
@rayflyers
@rayflyers 2 жыл бұрын
0:58 Look, mama, I'm famous!
@Riffomonas
@Riffomonas 2 жыл бұрын
Ha! Thanks for the question 🤓
@sven9r
@sven9r 2 жыл бұрын
you did what I never accomplished for years :D
@dreznik
@dreznik 11 ай бұрын
you do know is that to direct the output of |> to an arbitrary arg w a function: ... |> \(x) cor.test(..., x)
@user-cg8ey9rv4u
@user-cg8ey9rv4u 2 жыл бұрын
just use %>% 😃
@Riffomonas
@Riffomonas 2 жыл бұрын
🤓
@djangoworldwide7925
@djangoworldwide7925 2 жыл бұрын
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.
A head to head comparison of the base R and magrittr pipe (CC244)
21:34
Riffomonas Project
Рет қаралды 3 М.
Magic trick 🪄😁
00:13
Andrey Grechka
Рет қаралды 50 МЛН
Kids' Guide to Fire Safety: Essential Lessons #shorts
00:34
Fabiosa Animated
Рет қаралды 15 МЛН
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 17 МЛН
TidyTuesday: Comparing TidyModels with Caret
41:36
Andrew Couch
Рет қаралды 7 М.
R 4.1.0: Base R Pipe Has Arrived! Use Cases, Benchmark vs. magrittr
11:15
Teaching the tidyverse in 2023 | Mine Çetinkaya-Rundel
37:55
Posit PBC
Рет қаралды 17 М.
Using renv to track the version of your packages in R (CC229)
20:33
Riffomonas Project
Рет қаралды 7 М.
Lubridate - how to manipulate date and time data in R
18:05
R Programming 101
Рет қаралды 10 М.
Using dplyr's group_by function with and without summarize (CC233)
25:54
Riffomonas Project
Рет қаралды 4,8 М.
Indexing and looping over lists in R, a tutorial (CC174)
19:59
Riffomonas Project
Рет қаралды 4,9 М.
Электронный звонок #shorts
0:26
TheBestBike
Рет қаралды 497 М.
USB-C ИЛИ THUNDERBOLT | А ЕСТЬ ЛИ РАЗНИЦА?
19:54