R Package Review Episode 1: Magrittr

Bryan Jenks · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

The Magrittr package in R provides a pipe operator for workflow management and data manipulation, compatible with functions that start with the first argument being the data the function is operating on. The package offers four pipes: normal, compound, exposition, and tee pipe, useful for various data processing tasks.

Full Transcript

what's going on everybody so today is the first our package review I'm going to be doing on this channel and hopefully this is one of many to come in the future and so today we're gonna start off with Magritte our or the package that gave our the pipe so the pipe is basically the UNIX pipe in a way it takes stuff on the left hand side left hand side and puts it into the function on the right hand side so how this works in our is if you have a data frame you have some value and you have a data frame and you want to take that data frame and put that into a function you can do that the pipe pipe says take the data frame put it into the function and it puts it into the function as the first argument of the function so these functions that all start with the first argument being the data the function is going to be operating on are compatible with the pipe now anything that doesn't have that I think that that causes an issue but correct me if I'm wrong now with the pipe you can say data frame into a function and then you know the function works and if a data frame is returned from that function and it just performs a something on that that data frame then if you pipe the result of that function to another function you're basically just passing your data frame along in through various stages of processing and in the tidy verse using D plier to filter out criteria or to select things aggregate mutate etc this is incredibly useful and I really like that workflow I like you taking you know my data frame and then shoving it through multiple deep layer functions to filter it aggregate group or mutate etc and at the end I have another data frame now this doesn't just work with just the tidy verse now you can take other things and do other operations with it so in this package review series I'll actually be going through various examples of some of the more commonly used functions in these packages and with Magritte it's going to be the greet our it's gonna be the four pipes that are offered that's all I'm going to cover from this package so the library is just Magritte are now with the tidy verse a lot of the tidy verse packages automatically import the actual pipe function by standard so you don't you rarely need to straight up load the package but I have encountered situations multiple in which I actually have needed to import the library for the pipe but excuse me but let's get into it so the normal pipe is just the % less than re greater than and then another % that's a pain to type which is why there is a shortcut in our studio I have the vim settings so I'm going to enter insert mode so if you're just typing in our studio you can do ctrl shift m' so ctrl shift m' and that will insert a pipe character so if i do ctrl shift m' that will insert a pipe really useful if you're just typing in your function so you got your data ctrl shift m' next function ctrl shift m' next function you don't need to like break your stride i mean during your analysis to use this this function and use the pipe so in this case my first example here is just a vector of numerical values just a vector of numbers and i'm taking this vector and piping it into the mean function which is basically just saying take this and put it into this function return the mean of this vector so when i run that now the cool thing with the pipe is if you have a bunch of pipe connected operations on any of those lines if you do control enter it'll run the entire pipe operation so i don't need to select it all i don't need to run the whole chunk either i could just say you know here or here or wherever just control enter and it'll run and I get my mean of those values so then I could also just interest to show you how that's working mean put the vector in there same result that's basically just doing what I typed right here but it looks cleaner you take this vector you pipe it into the function and it's just like the UNIX pipe in that respect I accept that this one you can actually have line breaks so right here you could just say like this is basically in if this was like you know bash or something you know pipe it into that it's pretty much what's going on now you don't have to do just standard input and output because this is our you can take whole data structures you can do vectors you can do data frames Tibbals tribbles whatever and you can do the same sort of operation you can shove the value or function result into another function and with passing data frames or data tables you can get a lot of like functionality out of this and I use this on the daily using the pipe character is like critical to my typical workflow nowadays so what happens if we also have an assignment so I have the result of that mean formula going into X right here but how do I do with this like I'm X's being assigned the value of this vector but that vector is being piped into the mean function how do you understand this now at first glance this might look weird but oh the way you can read this and understand it is by reading it this way X is receiving the value of the vector after having gone through the mean function so the mean of the vector is being assigned to X and that's basically how I read these functions you could get rid of this entirely and you're just getting you know the result of the main function which is that you know for one repeating and that equals this result and so that result is what's being assigned to X so and I put the verbage right there for you to so you can read it that way as well and so when you do that and you run the result of this you know AG the value of X is the same thing it's just assigning that end value to X this will work with a data frame it'll work with a single summary statistic etc now if I just run just this portion I don't run the concat formula and I just run that I don't get any output because the output is being assigned to the x value which you can see up here in my environment so it gets assigned to the environmental variable X which is y if I selected this it'll actually print it out on what it is which is for one repeating so if you would do an assignment this way you won't receive console output except when you do the next pipe actually I think that's actually one of the other pipes later anyways I digress so that is just the normal pipe this one is the one with the control shift M hotkey and that is the most typically used one now there are other pipes and other with other functionality but that is the most typically used pipe character piping something forward into the next operation or function and I use that endlessly now the next pipe is the compound pipe this one starts with percent less than greater than percent again you can see all these up here as well now this no normal pipe will push something forward into the process the compound pipe pushes something forward into the process but then also assigns that back to its initial value so right here you can see that I'm assigning this vector the same numerical vector to Y so I'm gonna do that so now in our environment you can see that we have a numerical vector assigned to Y now if I use the compound pipe a pipe and I say Y that is equal to this vector is being piped into the mean function but then the result of that function is being piped back in to Y and then I will be a Y will be reassigned the value of the result of the mean function on that vector which is a real big mouthful for really just the operation occurring is you know why is a sign that vector can't type today why is a signed mean of why this is really what's occurring with using the compound pipe above this that's that's basically what it is so let's run that and watch the environment up here so you can see there right now it's the vector but when I run the compound pipe it receives the result of its operations so it's saying hey take the value shove it forward operate and take the result and put it back into y and reassign Y to that result so when we see the result it is in fact the result of the mean operation and that's the compound pipe now honestly I wouldn't really use this because I don't like overwriting my variables like this now when you pipe something forward it's basically like read-only mode on the actual variable itself like if I had the vector still assigned to Y and I only pipe this forward into the mean if I did the result of the mean function you know I'd get 4.1 repeating but the value of y is still that vector so it's like hey I'm gonna borrow a copy of this and then operate on it but the original variable retains its initial value so I don't like overwriting my variables this way so I probably would not ever really use this I've never used it to date so far but you know the functionality of this exists if you want to I mean at some point you know are you gonna really abstract your code so much that it's harder to understand or is legibility and readability more important at that level I mean and this type of operation you might just want to err on the side of legibility but I digress the next pipe is the exposition pipe now I didn't learn about this until I was making this outline for this video this is actually a really cool one and useful I haven't used it yet but and I professionally how it used to get professionally but I do think it this is actually really useful so if I'm gonna have a Tribble now ignore all the verb here I'm basically just doing a one-off set of tabular data with two columns four rows yeah that's how you can think about this so if I take this Tribble which is just two columns of name and age for you know I have a character vector and I have a numerical or double vector or whatever a numerical vector and a character vector if I just pipe this whole Tribble into the mean function and say hey I want to do the mean of age because I obviously can't do the mean over a character vector if I try to run this uh-oh something broke and doesn't work so at this point it's assuming I'm trying to do mean over both columns because it doesn't even recognize age it doesn't even know what age is because age is over here in the triple the mean function doesn't know what age is because we're piping this forward and I don't only know a better way of explaining that other than with this example is that the exposition pipe will expose the names of these columns or these variables to the function it is being piped into so it's saying hey I'm gonna be sending you some data here are the column headers so that you know in advance of the operation to occur so in this case it's saying you know here's the Tribble it exists now and we're sending it the names so now it recognizes o age exists okay we're gonna be operating on age only and then the data arrives okay we're operating on age only and so when I run this operation it does return the mean age so this would be a way of recognizing the variable names of a data structure entering a function before that data structure gets there it would necessarily it would save a step like what I'm envisioning is that this would save a step on a deep liar filter where I just take this Tribble or a data frame and select a variable with deep liar select and then pipe that into the mean function it saves that step it's just hey just here's the columns age only please do the mean of that and this there's your answer there's what that's what you wanted to know and that would just get that the way so we could save a step and it's percent dollar signs in percent and that's the the third pipe now the final and last pipe is the tea pipe now I thought this was really interesting and I was wondering what is a use case for this until I saw the actual like listed standard use case which is like sending a data frame or something to a plot now if you have like a two vector data frame or two column two variable data frame of I don't know two continuous variables and you pipe that into giome point or something as ggplot you could have a scatter plot just produced but you also want console output so the tea pipe works in this way and I drew like a little bit of a ASCII art graphic for you you take your value this could be your vector your data frame whatever your value is and you're gonna pipe this forward into a function and the function is going to do something now if it's just like the mean function now it's not going to actually output anything so the function will just operate and it will also send the same value down the tea to be returned in the console so if I did a vector it would be returning the vector into the console and then the function will operate on that vector so a good example of this is the two functions I have below here so when I pipe the when I use the tea pipe to take the vector and put it into me I should see four point one repeating right well when I run that I don't the only thing returned is the vector the mean function did operate a mean was calculated but it's not returned into the console because of the tea pipe but when we pipe this vector the same vector into the plot function it will not only output the vector itself into the console it'll actually shove the same vector into the plot function and plot it let's do that and we can see that not only do we have console output but we also have a plot now the plot was produced by using the index or the places you know the first character first value for a second value third value and the actual value itself so is what it's saying index there but I digress you have a plot and you have the vector console output so that's the tea pipe when you want to have something operated on by a function but don't want console output but you also want to output the original value that was being operated upon so those are the four pipes you have the normal you have the compound the exposition and the tea pipe and that yeah these are very very useful I use the normal pipe every day all day for my operations especially deep lyre functions I really enjoy that workflow so that is from the Magritte R package and that's the pipe have fun with that bye

Original Description

▬▬▬▬▬▬▬▬▬▬ ► CHECK THESE OUT ◀︎▬▬▬▬▬▬▬▬▬▬ 📧️ NEWSLETTER: https://bryanjenkstech.ck.page/d4ec0713d5 💬 DISCORD: https://discord.gg/MxCVshN 🗣️ SOCIALS: https://streamerlinks.com/tallguyjenks ▬▬▬▬▬▬▬▬▬▬ ► SUPPORT THE CHANNEL ◀︎▬▬▬▬▬▬▬▬▬▬ 👨🏻‍💻️ GITHUB SPONSOR: https://github.com/sponsors/tallguyjenks 🙏🏻️ AMAZON WISHLIST: https://www.amazon.com/hz/wishlist/ls/17FRLE35NC7G8?ref_=wl_share 😇 PATREON: https://www.patreon.com/bryanjenks?fan_landing=true 🙌🏻️ YOUTUBE MEMBERSHIP: https://www.youtube.com/c/BryanJenksTech/join ☕ BUY ME A COFFEE: https://www.buymeacoffee.com/tallguyjenks 💵 PAYPAL: https://www.paypal.me/tallguyjenks 📊️ FREE STOCKS: http://join.robinhood.com/bryanj67 ▬▬▬▬▬▬▬▬▬▬ ► My Newsletter ◀︎▬▬▬▬▬▬▬▬▬▬ 📧️ NEWSLETTER: https://bryanjenkstech.ck.page/d4ec0713d5 ▬▬▬▬▬▬▬▬▬▬ ► My Gear ◀︎▬▬▬▬▬▬▬▬▬▬ ⚙️GEAR: https://kit.co/tallguyjenks/my-gear ▬▬▬▬▬▬▬▬▬▬ ► Questions? ◀︎▬▬▬▬▬▬▬▬▬▬ ❓️FAQ: https://github.com/BryanJenksCommunity/FAQ/discussions ▬▬▬▬▬▬▬▬▬▬ ► Social ◀︎▬▬▬▬▬▬▬▬▬▬ 💬 DISCORD: https://discord.gg/MxCVshN 🐦 TWITTER: https://twitter.com/tallguyjenks 📺 TWITCH: https://www.twitch.tv/tallguyjenks 📜️ MEDIUM: https://medium.com/@tallguyjenks 💼️ LINKEDIN: https://www.linkedin.com/in/bryanjenks/ 🖥️ GITHUB: https://github.com/tallguyjenks 🌎 WEBSITE: https://www.bryanjenks.dev/ ▬▬▬▬▬▬▬▬▬▬ ► The Rest ◀︎▬▬▬▬▬▬▬▬▬▬ Thanks for watching and if you liked this video please leave a 👍🏻 Subscribe to my channel and click the 🔔 icon for notifications when I post a new video If you read this far put a 🐄 in the comments! ▬▬▬▬▬▬▬▬▬▬ ► TAGS ◀︎▬▬▬▬▬▬▬▬▬▬ #obsidian #zettelkasten #bryanjenks 0:00:00 Welcome 0:00:23 Intro 0:01:37 SUPPORT THE CHANNEL 0:01:57 Diving In 0:03:08 Installation 0:03:24 Using ymlthis 0:10:04 Using an RMarkdown Template 0:11:29 Closing 0:12:07 THANK YOU SPONSORS 1:00:33 What we are talking about today 1:12:32 Outro
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Bryan Jenks · Bryan Jenks · 18 of 60

1 rsync for Linux Backups - The Final Barrier to Migration
rsync for Linux Backups - The Final Barrier to Migration
Bryan Jenks
2 (un)Installing Packages From The (AUR) Arch Linux User Repository
(un)Installing Packages From The (AUR) Arch Linux User Repository
Bryan Jenks
3 Full RStudio Set Up On Arch Linux
Full RStudio Set Up On Arch Linux
Bryan Jenks
4 Fix RMarkdown Compilation Outside Of RStudio on Arch Linux
Fix RMarkdown Compilation Outside Of RStudio on Arch Linux
Bryan Jenks
5 Markdown Document Autocompilation on Arch Linux
Markdown Document Autocompilation on Arch Linux
Bryan Jenks
6 Cronjobs, Cronie, & Crontab on Arch Linux
Cronjobs, Cronie, & Crontab on Arch Linux
Bryan Jenks
7 Setting Up Slack With i3 on Arch Linux
Setting Up Slack With i3 on Arch Linux
Bryan Jenks
8 VS Codium (VS Code) on Arch Linux With A Shell Script
VS Codium (VS Code) on Arch Linux With A Shell Script
Bryan Jenks
9 Vimwiki Plugin For Vim Research Management on Arch Linux
Vimwiki Plugin For Vim Research Management on Arch Linux
Bryan Jenks
10 Neomutt with Protonmail on Arch Linux - LARBS - Thinkpad x220
Neomutt with Protonmail on Arch Linux - LARBS - Thinkpad x220
Bryan Jenks
11 Command Line Task Management With Taskwarrior and Taskell On Arch Linux / Thinkpad x220
Command Line Task Management With Taskwarrior and Taskell On Arch Linux / Thinkpad x220
Bryan Jenks
12 Exploring My Fitbit Data With R in RStudio on Arch Linux
Exploring My Fitbit Data With R in RStudio on Arch Linux
Bryan Jenks
13 Tellico Collections On Arch Linux
Tellico Collections On Arch Linux
Bryan Jenks
14 LaTeX, Biber, and Live Compilation on Arch Linux
LaTeX, Biber, and Live Compilation on Arch Linux
Bryan Jenks
15 R Markdown Programming Language Support
R Markdown Programming Language Support
Bryan Jenks
16 R Markdown to make HTML Wiki's with Tabbed pages
R Markdown to make HTML Wiki's with Tabbed pages
Bryan Jenks
17 Announcement: New Video Series on R - "Comprehensive R Package Reviews"
Announcement: New Video Series on R - "Comprehensive R Package Reviews"
Bryan Jenks
R Package Review Episode 1: Magrittr
R Package Review Episode 1: Magrittr
Bryan Jenks
19 R Package Review Episode 2: Vitae
R Package Review Episode 2: Vitae
Bryan Jenks
20 My R Workflow for Reproduce-able & Portable Analysis
My R Workflow for Reproduce-able & Portable Analysis
Bryan Jenks
21 R Package Review Episode 2: Here
R Package Review Episode 2: Here
Bryan Jenks
22 Introduction to Regular Expressions
Introduction to Regular Expressions
Bryan Jenks
23 My Workflow for Reading, Organizing, and Maintaining Articles, Papers, & Books
My Workflow for Reading, Organizing, and Maintaining Articles, Papers, & Books
Bryan Jenks
24 My First Python Project Dealing With Finance Data
My First Python Project Dealing With Finance Data
Bryan Jenks
25 R Package Review Episode 4: Beepr
R Package Review Episode 4: Beepr
Bryan Jenks
26 RMarkdown Customized Styles with CSS and HTML Output
RMarkdown Customized Styles with CSS and HTML Output
Bryan Jenks
27 RMarkdown Custom ID Selectors for Dynamic Headers and CSS
RMarkdown Custom ID Selectors for Dynamic Headers and CSS
Bryan Jenks
28 HTML Headers in RMarkdown Documents For Personal/Corporate Branding
HTML Headers in RMarkdown Documents For Personal/Corporate Branding
Bryan Jenks
29 My Semi-Complete VimWiki Workflow
My Semi-Complete VimWiki Workflow
Bryan Jenks
30 How To Make An Automated Resume With Github
How To Make An Automated Resume With Github
Bryan Jenks
31 How I Use Fuzzy Finding In the Terminal with fzf (workflow++)
How I Use Fuzzy Finding In the Terminal with fzf (workflow++)
Bryan Jenks
32 How I Organize and Create My Research Notes (Research Workflow++)
How I Organize and Create My Research Notes (Research Workflow++)
Bryan Jenks
33 How I Use fzf.vim To Improve My Programming Workflow
How I Use fzf.vim To Improve My Programming Workflow
Bryan Jenks
34 Website Updates, JavaScript, R, Shiny, Vue.js And More
Website Updates, JavaScript, R, Shiny, Vue.js And More
Bryan Jenks
35 How To Use AWK (Tutorial)
How To Use AWK (Tutorial)
Bryan Jenks
36 Bash Script Review: My Battery Power i3Blocks Module
Bash Script Review: My Battery Power i3Blocks Module
Bryan Jenks
37 Channel Updates, Where I've Been, And Where I Want To Go With YouTube
Channel Updates, Where I've Been, And Where I Want To Go With YouTube
Bryan Jenks
38 How To Use Neomutt 📨 From MuttWizard  (Basics Tutorial)
How To Use Neomutt 📨 From MuttWizard (Basics Tutorial)
Bryan Jenks
39 How To Use Jupyter Notebooks 📔 (Basics Tutorial)
How To Use Jupyter Notebooks 📔 (Basics Tutorial)
Bryan Jenks
40 How To Use Trello In 2020 (The Definitive Guide)
How To Use Trello In 2020 (The Definitive Guide)
Bryan Jenks
41 Macbook Pro 16 Inch 2020: Unboxing and Review
Macbook Pro 16 Inch 2020: Unboxing and Review
Bryan Jenks
42 How To Use Github's New Personal README and Wakatime
How To Use Github's New Personal README and Wakatime
Bryan Jenks
43 How I Set Up My 2020 Macbook Pro 16
How I Set Up My 2020 Macbook Pro 16
Bryan Jenks
44 My First Week At WGU (Western Governors University), Coffee, And Channel Updates
My First Week At WGU (Western Governors University), Coffee, And Channel Updates
Bryan Jenks
45 The Best Academic Resources & Citation Managers: OrcID, Zotero, Mendeley & More!
The Best Academic Resources & Citation Managers: OrcID, Zotero, Mendeley & More!
Bryan Jenks
46 R Package Review Episode 5: TodoR
R Package Review Episode 5: TodoR
Bryan Jenks
47 R Package Review Episode 6: Patchwork
R Package Review Episode 6: Patchwork
Bryan Jenks
48 Interview With Bryan of Norseman Leather Works
Interview With Bryan of Norseman Leather Works
Bryan Jenks
49 Zettelkasten Work in Obsidian for Research | VOD
Zettelkasten Work in Obsidian for Research | VOD
Bryan Jenks
50 How I Live With Adult ADHD (Attention Deficit Hyperactivity Disorder) [Time Stamped]
How I Live With Adult ADHD (Attention Deficit Hyperactivity Disorder) [Time Stamped]
Bryan Jenks
51 Zettelkasten Research Work in Obsidian | VOD
Zettelkasten Research Work in Obsidian | VOD
Bryan Jenks
52 Obsidian VS Roam Research: Why I Chose Obsidian
Obsidian VS Roam Research: Why I Chose Obsidian
Bryan Jenks
53 My 2020 Comprehensive Obsidian Workflow For Zettelkasten and Evergreen Notes
My 2020 Comprehensive Obsidian Workflow For Zettelkasten and Evergreen Notes
Bryan Jenks
54 How I Use Raindrop.io As The Entry Point of My Zettelkasten Workflow In Obsidian
How I Use Raindrop.io As The Entry Point of My Zettelkasten Workflow In Obsidian
Bryan Jenks
55 Comprehensive Overview | Obsidian Block References & Transclusion | Sorry Roam!
Comprehensive Overview | Obsidian Block References & Transclusion | Sorry Roam!
Bryan Jenks
56 Easy YouTube Timestamps From Final Cut Pro X With Python!
Easy YouTube Timestamps From Final Cut Pro X With Python!
Bryan Jenks
57 NEW | Obsidian Insiders Release 0.9.10 | Plugins & Official API
NEW | Obsidian Insiders Release 0.9.10 | Plugins & Official API
Bryan Jenks
58 TOP 5️⃣️ | FAVORITE THINGS IN OBSIDIAN
TOP 5️⃣️ | FAVORITE THINGS IN OBSIDIAN
Bryan Jenks
59 Comprehensive Obsidian & Git Sync Workflow 🔄️ | Your Mind Under Version Control
Comprehensive Obsidian & Git Sync Workflow 🔄️ | Your Mind Under Version Control
Bryan Jenks
60 Obsidian Mermaid Livestream Highlights | Zettelkasten Resources, YouTube Advice, Data Science
Obsidian Mermaid Livestream Highlights | Zettelkasten Resources, YouTube Advice, Data Science
Bryan Jenks

The Magrittr package in R provides a powerful tool for data manipulation and workflow management through its pipe operator. By using the pipe operator, users can create efficient workflows and perform various data processing tasks. The package offers four pipes, each with its own unique functionality, making it a versatile tool for data analysis.

Key Takeaways
  1. Load the Magrittr package
  2. Use the pipe operator to pass data from the left-hand side to a function on the right-hand side
  3. Chain multiple functions together using the pipe operator
  4. Use the shortcut pipe (ctrl+shift+m') to insert a pipe character
  5. Use the compound pipe for reassignment and feedback
  6. Assign result of function to variable
  7. Use the exposition pipe to expose names of columns or variables
  8. Use the tee pipe for console output and plotting
💡 The Magrittr package provides a flexible and efficient way to perform data manipulation and workflow management in R, making it a valuable tool for data analysts and scientists.

Related AI Lessons

Chapters (11)

Welcome
0:23 Intro
1:37 SUPPORT THE CHANNEL
1:57 Diving In
3:08 Installation
3:24 Using ymlthis
10:04 Using an RMarkdown Template
11:29 Closing
12:07 THANK YOU SPONSORS
1:00:33 What we are talking about today
1:12:32 Outro
Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →