R Tutorial : Y kant I reed ur code?
Key Takeaways
This video tutorial covers best practices for writing functions in R, focusing on clear and descriptive naming conventions, proper argument ordering, and usability with the pipe operator.
Full Transcript
most variables represent objects that is they are nouns functions are a little different because they perform actions you can think of functions as verbs let's consider some functions from D plier for manipulating data frames select performs the action of selecting columns and filter performs the action of filtering rows notice that the words select and filter are verbs in fact it's good practice that all function names should contain a verb if you're stuck for ideas this list should get you started LM is perhaps the most high-profile badly named function first of all is an acronym so you have to read the documentation to determine that LM means linear model secondly it doesn't contain a verb thirdly there are lots of types of linear model and it isn't obvious from the name that this runs a linear regression I prefer a more literal name like run linear regression one possible counter-argument is that run linear regression is more effort type than LM that's true but there are seven good reasons why that doesn't really matter firstly the amount of time spent reading and understanding code is almost always longer than the time spent to type it I'm a slow typist but I can type run linear regression quicker than opening a help page for LM and reading it to figure out what the function does secondly every modern code editor will autocomplete function names in the data camp script pane start by typing the name of a function and press tab you can select an option without having to type the whole function name thirdly you can assign functions just like any other variable type I found myself calling head a lot so I define H to be vehicle to head and it saves me a few keystrokes as well as naming your function to make it easy to use you have to put the arguments in a sense order there are two types of function arguments data arguments are the things that you compute on and detail arguments tell the function how to perform the computation for example looking at the arguments of core for calculating correlations x and y are data arguments and use and method our detail arguments LM has another problem formula is a detail argument so data should precede it since data isn't first LM doesn't play nicely with a pipe operator and this code will throw an error of course LM has an excuse since it was written several decades before the pipe operator existed since we've established that LM has problems we can write a wrapper function run linear regression has a clear name the data argument comes first and it works with pipes time to name your own from
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/introduction-to-writing-functions-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Most variables represent objects. That is, they are "nouns". Functions are a little different because they perform actions. You can think of functions as verbs.
Let's consider some functions from dplyr for manipulating data frames.
select performs the action of selecting columns, and filter performs the action of filtering rows.
Notice that the words select and filter are verbs.
In fact, it is good practice that all function names should contain a verb.
If you are stuck for ideas, this list should get you started.
lm is perhaps the most high-profile badly named function.
First of all, it's an acronym, so you have to read the documentation to determine that lm means "linear model".
Secondly, it doesn't contain a verb.
Thirdly, there are lots of types of linear model, and it isn't obvious from the name that this runs a linear regression.
I'd prefer a more literal name like "run_linear_regression".
One possible counterargument is that run_linear_regression is more effort to type than lm.
That's true, but there are some good reasons why that doesn't really matter.
Firstly, the amount of time spent reading and understanding code is almost always longer than the time to type it. I'm a slow typist, but I can type run_linear_regression quicker than opening a help page for lm and reading it to figure out what the function does.
Secondly, every modern code editor will auto-complete function names.
In the DataCamp script pane, start typing the name of a function and press TAB. You can select an option without having to type the whole function name.
Thirdly, you can assign functions just like any other variable type.
I find myself calling head a lot, so I define h to be equal to head, and it saves me a few keystrokes.
As well as na
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
🎓
Tutor Explanation
DeepCamp AI