About this ebook
R is a programming language developed is widely used for statistical and graphical analysis. It can execute advance machine learning algorithms including earning algorithm, linear regression, time series, statistical inference.
R programming language is used by Fortune 500 companies and tech bellwethers like Uber, Google, Airbnb, Facebook, Apple.
R provides a data scientist tools and libraries (Dplyr) to perform the 3 steps of analysis 1) Extract 2) Transform, Cleanse 3) Analyze.
Table of Contents
Chapter 1: What is R Programming Language? Introduction & Basics
Chapter 2: How to Download & Install R, RStudio, Anaconda on Mac or Windows
Chapter 3: R Data Types, Arithmetic & Logical Operators with Example
Chapter 4: R Matrix Tutorial: Create, Print, add Column, Slice
Chapter 5: Factor in R: Categorical & Continuous Variables
Chapter 6: R Data Frame: Create, Append, Select, Subset
Chapter 7: List in R: Create, Select Elements with Example
Chapter 8: R Sort a Data Frame using Order()
Chapter 9: R Dplyr Tutorial: Data Manipulation(Join) & Cleaning(Spread)
Chapter 10: Merge Data Frames in R: Full and Partial Match
Chapter 11: Functions in R Programming (with Example)
Chapter 12: IF, ELSE, ELSE IF Statement in R
Chapter 13: For Loop in R with Examples for List and Matrix
Chapter 14: While Loop in R with Example
Chapter 15: apply(), lapply(), sapply(), tapply() Function in R with Examples
Chapter 16: Import Data into R: Read CSV, Excel, SPSS, Stata, SAS Files
Chapter 17: How to Replace Missing Values(NA) in R: na.omit & na.rm
Chapter 18: R Exporting Data to Excel, CSV, SAS, STATA, Text File
Chapter 19: Correlation in R: Pearson & Spearman with Matrix Example
Chapter 20: R Aggregate Function: Summarise & Group_by() Example
Chapter 21: R Select(), Filter(), Arrange(), Pipeline with Example
Chapter 22: Scatter Plot in R using ggplot2 (with Example)
Chapter 23: How to make Boxplot in R (with EXAMPLE)
Chapter 24: Bar Chart & Histogram in R (with Example)
Chapter 25: T Test in R: One Sample and Paired (with Example)
Chapter 26: R ANOVA Tutorial: One way & Two way (with Examples)
Chapter 27: R Simple, Multiple Linear and Stepwise Regression [with Example]
Chapter 28: Decision Tree in R with Example
Chapter 29: R Random Forest Tutorial with Example
Chapter 30: Generalized Linear Model (GLM) in R with Example
Chapter 31: K-means Clustering in R with Example
Chapter 32: R Vs Python: What's the Difference?
Chapter 33: SAS vs R: What's the Difference?
Read more from Alex Nordeen
Learn SAP Basis in 24 Hours Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Learn SAP MM in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn SQL in 24 Hours Rating: 5 out of 5 stars5/5Learn MongoDB in 24 Hours Rating: 5 out of 5 stars5/5Learn Operating System in 24 Hours Rating: 0 out of 5 stars0 ratingsLinux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Learn HANA in 24 Hours Rating: 5 out of 5 stars5/5Learn SAP SD in 24 Hours Rating: 5 out of 5 stars5/5Learn SAP HR in 24 Hours Rating: 5 out of 5 stars5/5Learn Software Testing in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn PMP in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn Data Warehousing in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn Selenium in 24 Hours Rating: 0 out of 5 stars0 ratingsC++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsC# for Beginners: Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn SQLite in 24 Hours Rating: 0 out of 5 stars0 ratingsBusiness Analysis : Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn SAP BI in 24 Hours Rating: 3 out of 5 stars3/5Learn Excel in 24 Hours Rating: 4 out of 5 stars4/5Learn VBScript in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn PHP in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn C Programming in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn Design and Analysis of Algorithms in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn Hadoop in 24 Hours Rating: 0 out of 5 stars0 ratingsHacking : Guide to Computer Hacking and Penetration Testing Rating: 5 out of 5 stars5/5Learn AngularJS in 24 Hours Rating: 0 out of 5 stars0 ratingsLearn Cassandra in 24 Hours Rating: 0 out of 5 stars0 ratings
Related to Learn R Programming in 24 Hours
Related ebooks
R for Data Science Rating: 5 out of 5 stars5/5R Programming - a Comprehensive Guide: Software Rating: 0 out of 5 stars0 ratingsData Preparation and Exploration: Applied to Healthcare Data Rating: 0 out of 5 stars0 ratingsMastering Python Data Analysis Rating: 0 out of 5 stars0 ratingsThe Data Science Workshop: A New, Interactive Approach to Learning Data Science Rating: 0 out of 5 stars0 ratingsBayesian Analysis with Python Rating: 4 out of 5 stars4/5Introduction to Statistics: An Intuitive Guide for Analyzing Data and Unlocking Discoveries Rating: 5 out of 5 stars5/5Biostatistics by Example Using SAS Studio Rating: 0 out of 5 stars0 ratingsLearning Bayesian Models with R Rating: 5 out of 5 stars5/5Introduction to R for Business Intelligence Rating: 0 out of 5 stars0 ratingsMicrosoft Azure Machine Learning Rating: 4 out of 5 stars4/5End-to-End Data Science with SAS: A Hands-On Programming Guide Rating: 0 out of 5 stars0 ratingsWeb Application Development with R Using Shiny - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Predictive Analytics with Python Rating: 4 out of 5 stars4/5Multivariate Analysis – The Simplest Guide in the Universe: Bite-Size Stats, #6 Rating: 0 out of 5 stars0 ratingsBeginner’s Guide to Correlation Analysis: Bite-Size Stats, #4 Rating: 0 out of 5 stars0 ratingsPractical Data Science Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMachine Learning For Beginners Guide Algorithms: Supervised & Unsupervsied Learning. Decision Tree & Random Forest Introduction Rating: 0 out of 5 stars0 ratingsThe Practically Cheating Statistics Handbook, The Sequel! (2nd Edition) Rating: 5 out of 5 stars5/5Surviving Statistics: A Professor's Guide to Getting Through Rating: 0 out of 5 stars0 ratingsStatistics Super Review, 2nd Ed. Rating: 5 out of 5 stars5/5Introduction to Applied Econometrics Analysis Using Stata Rating: 5 out of 5 stars5/5Machine Learning - A Complete Exploration of Highly Advanced Machine Learning Concepts, Best Practices and Techniques: 4 Rating: 0 out of 5 stars0 ratingsPredictive Analytics For Dummies Rating: 3 out of 5 stars3/5Learning R Programming Rating: 5 out of 5 stars5/5
Internet & Web For You
The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5Build a WordPress Website From Scratch 2024: WordPress 2024 Rating: 0 out of 5 stars0 ratingsAdvanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsGet Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5Responsive Web Design with HTML5 and CSS3 Essentials Rating: 5 out of 5 stars5/5Canva Tips and Tricks Beyond The Limits Rating: 3 out of 5 stars3/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5PHP and MySQL For Dummies Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Learn PHP in 24 Hours Rating: 0 out of 5 stars0 ratingsiCloud for Beginners: A Ridiculously Simple Guide to Online Storage Rating: 0 out of 5 stars0 ratingsMastering Postman: A Comprehensive Guide to Building End-to-End APIs with Testing, Integration and Automation Rating: 0 out of 5 stars0 ratingsPublishing E-Books For Dummies Rating: 4 out of 5 stars4/5CISM Certified Information Security Manager Study Guide Rating: 4 out of 5 stars4/5A Pocket Guide to CSS Animations Rating: 5 out of 5 stars5/5UX/UI Design Playbook Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5JavaScript: Beginner's Guide to Programming Code with JavaScript Rating: 5 out of 5 stars5/5Coding For Dummies Rating: 5 out of 5 stars5/5REMOTE ACCESS VPN- SSL VPN: A deep dive into SSL VPN from basic Rating: 5 out of 5 stars5/5HTML, CSS, & JavaScript All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsSpring MVC Beginner’s Guide Rating: 2 out of 5 stars2/5JavaScript: Tips and Tricks to Programming Code with Javascript Rating: 0 out of 5 stars0 ratingsResponsive Web Design with HTML5 and CSS3 - Second Edition Rating: 4 out of 5 stars4/5React Projects: Build 12 real-world applications from scratch using React, React Native, and React 360 Rating: 0 out of 5 stars0 ratings
Reviews for Learn R Programming in 24 Hours
0 ratings0 reviews
Book preview
Learn R Programming in 24 Hours - Alex Nordeen
Learn R Programming in 24 Hours
By Alex Nordeen
Copyright 2021 - All Rights Reserved – Alex Nordeen
ALL RIGHTS RESERVED. No part of this publication may be reproduced or transmitted in any form whatsoever, electronic, or mechanical, including photocopying, recording, or by any informational storage or retrieval system without express written, dated and signed permission from the author.
Table Of Content
Chapter 1: What is R Programming Language? Introduction & Basics
What is R?
What is R used for?
R by Industry
R package
Communicate with R
Why use R?
Should you choose R?
Is R difficult?
Chapter 2: How to Download & Install R, RStudio, Anaconda on Mac or Windows
Install Anaconda
Install R
Install Rstudio
Run Rstudio
Test
Install package
Open a library
Run R code
Chapter 3: R Data Types, Arithmetic & Logical Operators with Example
Basic data types
Variables
Vectors
Arithmetic Operators
Logical Operators
Chapter 4: R Matrix Tutorial: Create, Print, add Column, Slice
What is a Matrix?
How to Create a Matrix in R
Add a Column to a Matrix with the cbind()
Slice a Matrix
Chapter 5: Factor in R: Categorical & Continuous Variables
What is Factor in R?
Categorical Variables
Continuous Variables
Chapter 6: R Data Frame: Create, Append, Select, Subset
What is a Data Frame?
How to Create a Data Frame
Slice Data Frame
Append a Column to Data Frame
Select a Column of a Data Frame
Chapter 7: List in R: Create, Select Elements with Example
How to Create a List
Select Elements from List
Built-in Data Frame
Chapter 8: R Sort a Data Frame using Order()
Chapter 9: R Dplyr Tutorial: Data Manipulation(Join) & Cleaning(Spread)
Introduction to Data Analysis
Merge with dplyr()
Data Cleaning functions
gather()
spread()
separate()
unite()
Chapter 10: Merge Data Frames in R: Full and Partial Match
Full match
Partial match
Chapter 11: Functions in R Programming (with Example)
What is a Function in R?
R important built-in functions
General functions
Math functions
Statistical functions
Write function in R
When should we write function?
Functions with condition
Chapter 12: IF, ELSE, ELSE IF Statement in R
The if else statement
The else if statement
Chapter 13: For Loop in R with Examples for List and Matrix
For Loop Syntax and Examples
For Loop over a list
For Loop over a matrix
Chapter 14: While Loop in R with Example
Chapter 15: apply(), lapply(), sapply(), tapply() Function in R with Examples
apply() function
lapply() function
sapply() function
Slice vector
tapply() function
Chapter 16: Import Data into R: Read CSV, Excel, SPSS, Stata, SAS Files
Read CSV
Read Excel files
readxl_example()
read_excel()
excel_sheets()
Import data from other Statistical software
Read sas
Read STATA
Read SPSS
Best practices for Data Import
Chapter 17: How to Replace Missing Values(NA) in R: na.omit & na.rm
Chapter 18: R Exporting Data to Excel, CSV, SAS, STATA, Text File
Export to Hard drive
Create data frame
Export CSV
Export to Excel file
Export to different software
Export SAS file
Export STATA file
R
Interact with the Cloud Services
Google Drive
Export to Dropbox
Chapter 19: Correlation in R: Pearson & Spearman with Matrix Example
Pearson Correlation
Spearman Rank Correlation
Correlation Matrix
Visualize Correlation Matrix
Chapter 20: R Aggregate Function: Summarise & Group_by() Example
Chapter 21: R Select(), Filter(), Arrange(), Pipeline with Example
select()
Filter()
Pipeline
arrange()
Chapter 22: Scatter Plot in R using ggplot2 (with Example)
ggplot2 package
Scatterplot
Change axis
Scatter plot with fitted values
Add information to the graph
Rename x-axis and y-axis
Control the scales
Theme
Save Plots
Chapter 23: How to make Boxplot in R (with EXAMPLE)
Create Box Plot
Basic box plot
Box Plot with Dots
Control Aesthetic of the Box Plot
Box Plot with Jittered Dots
Notched Box Plot
Chapter 24: Bar Chart & Histogram in R (with Example)
How to create Bar Chart
Bar chart: count
Customize the graph
Histogram
Chapter 25: T Test in R: One Sample and Paired (with Example)
What is Statistical Inference?
What is t-test?
One-sample t-test
Paired t-test
Chapter 26: R ANOVA Tutorial: One way & Two way (with Examples)
What is ANOVA?
One-way ANOVA
Pairwise comparison
Two-way ANOVA
Chapter 27: R Simple, Multiple Linear and Stepwise Regression [with Example]
Simple Linear regression
Machine learning
Chapter 28: Decision Tree in R with Example
What are Decision Trees?
Step 1) Import the data
Step 2) Clean the dataset
Step 3) Create train/test set
Step 4) Build the model
Step 5) Make a prediction
Step 6) Measure performance
Step 7) Tune the hyper-parameters
Chapter 29: R Random Forest Tutorial with Example
What is Random Forest in R?
Step 1) Import the data
Step 2) Train the model
Step 3) Search the best maxnodes
Step 4) Search the best ntrees
Step 5) Evaluate the model
Step 6) Visualize Result
Appendix
Chapter 30: Generalized Linear Model (GLM) in R with Example
What is Logistic regression?
How to create Generalized Liner Model (GLM)
Chapter 31: K-means Clustering in R with Example
What is Cluster analysis?
K-means algorithm
Optimal k
Chapter 32: R Vs Python: What’s the Difference?
R
Python
Popularity index
Job Opportunity
Analysis done by R and Python
Percentage of people switching
Difference between R and Python
R or Python Usage
Chapter 33: SAS vs R: What's the Difference?
What is SAS?
What is mean by R?
Why use SAS?
Why use R?
History of SAS
History of R
SAS Vs. R
Feature of R
Features of SAS
The Final Verdict
Chapter 1: What is R Programming Language? Introduction & Basics
What is R?
R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses an extensive catalog of statistical and graphical methods. It includes machine learning algorithm, linear regression, time series, statistical inference to name a few. Most of the R libraries are written in R, but for heavy computational task, C, C++ and Fortran codes are preferred.
R is not only entrusted by academic, but many large companies also use R programming language, including Uber, Google, Airbnb, Facebook and so on.
Data analysis with R is done in a series of steps; programming, transforming, discovering, modeling and communicate the results
Program: R is a clear and accessible programming tool
Transform: R is made up of a collection of libraries designed specifically for data science
Discover: Investigate the data, refine your hypothesis and analyze them
Model: R provides a wide array of tools to capture the right model for your data
Communicate: Integrate codes, graphs, and outputs to a report with R Markdown or build Shiny apps to share with the world
What is R used for?
Statistical inference
Data analysis
Machine learning algorithm
R by Industry
If we break down the use of R by industry, we see that academics come first. R is a language to do statistic. R is the first choice in the healthcare industry, followed by government and consulting.
R package
The primary uses of R is and will always be, statistic, visualization, and machine learning. The picture below shows which R package got the most questions in Stack Overflow. In the top 10, most of them are related to the workflow of a data scientist: data preparation and communicate the results.
All the libraries of R, almost 12k, are stored in CRAN. CRAN is a free and open source. You can download and use the numerous libraries to perform Machine Learning or time series analysis.
Communicate with R
R has multiple ways to present and share work, either through a markdown document or a shiny app. Everything can be hosted in Rpub, GitHub or the business's website.
Below is an example of a presentation hosted on Rpub
Rstudio accepts markdown to write a document. You can export the documents in different formats:
Document :
HTML
PDF/Latex
Word
Presentation
HTML
PDF beamer
Rstudio has a great tool to create an App easily. Below is an example of app with the World Bank data.
Why use R?
Data science is shaping the way companies run their businesses. Without a doubt, staying away from Artificial Intelligence and Machine will lead the company to fail. The big question is which tool/language should you use?
They are plenty of tools available in the market to perform data analysis. Learning a new language requires some time investment. The picture below depicts the learning curve compared to the business capability a language offers. The negative relationship implies that there is no free lunch. If you want to give the best insight from the data, then you need to spend some time learning the appropriate tool, which is R.
On the top left of the graph, you can see Excel and PowerBI. These two tools are simple to learn but don't offer outstanding business capability, especially in term of modeling. In the middle, you can see Python and SAS. SAS is a dedicated tool to run a statistical analysis for business, but it is not free. SAS is a click and run software. Python, however, is a language with a monotonous learning curve. Python is a fantastic tool to deploy Machine Learning and AI but lacks communication features. With an identical learning curve, R is a good trade-off between implementation and data analysis.
When it comes to data visualization (DataViz), you'd probably heard about Tableau. Tableau is, without a doubt, a great tool to discover patterns through graphs and charts. Besides, learning Tableau is not time-consuming. One big problem with data visualization is you might end up never finding a pattern or just create plenty of useless charts. Tableau is a good tool for quick visualization of the data or Business Intelligence. When it comes to statistics and decision-making tool, R is more appropriate.
Stack Overflow is a big community for programming languages. If you have a coding issue or need to understand a model, Stack Overflow is here to help. Over the year, the percentage of question-views has increased sharply for R compared to the other languages. This trend is of course highly correlated with the booming age of data science but, it reflects the demand of R language for data science.
In data science, there are two tools competing with each other. R and Python are probably the programming language that defines data science.
Should you choose R?
Data scientist can use two excellent tools: R and Python. You may not have time to learn them both, especially if you get started to learn data science. Learning statistical modeling and algorithm is far more important than to learn a programming language. A programming language is a tool to compute and communicate your discovery. The most important task in data science is the way you deal with the data: import, clean, prep, feature engineering, feature selection. This should be your primary focus. If you are trying to learn R and Python at the same time without a solid background in statistics, its plain stupid. Data scientist are not programmers. Their job is to understand the data, manipulate it and expose the best approach. If you are thinking about which language to learn, let's see which language is the most appropriate for you.
The principal audience for data science is business professional. In the business, one big implication is communication. There are many ways to communicate: report, web app, dashboard. You need a tool that does all this together.
Is R difficult?
Years ago, R was a difficult language to master. The language was confusing and not as structured as the other programming tools. To overcome this major issue, Hadley Wickham developed a collection of packages called tidyverse. The rule of the game changed for the best. Data manipulation become trivial and intuitive. Creating a graph was not so difficult anymore.
The best algorithms for machine learning can be implemented with R. Packages like Keras and TensorFlow allow to create high-end machine learning technique. R also has a package to perform Xgboost, one the best algorithm for Kaggle competition.
R can communicate with the other language. It is possible to call Python, Java, C++ in R. The world of big data is also accessible to R. You can connect R with different databases like Spark or Hadoop.
Finally, R has evolved and allowed parallelizing operation to speed up the computation. In fact, R was criticized for using only one CPU at a time. The parallel package lets you to perform tasks in different cores of the machine.
Summary
In a nutshell, R is a great tool to explore and investigate the data. Elaborate analysis like clustering, correlation, and data reduction are done with R. This is the most crucial part, without a good feature engineering and model, the deployment of the machine learning will not give meaningful results.
Chapter 2: How to Download & Install R, RStudio, Anaconda on Mac or Windows
R is a programming language. To use R, we need to install an Integrated Development Environment (IDE). Rstudio is the Best IDE available as it is user-friendly, open-source and is part of the Anaconda platform.
Install Anaconda
What is Anaconda?
Anaconda free open source is distributing both Python and R programming language. Anaconda is widely used in the scientific community and data scientist to carry out Machine Learning project or data analysis.
Why use Anaconda?
Anaconda will help you to manage all the libraries required for Python, or R. Anaconda will install all the required libraries and IDE into one single folder to simplify package management. Otherwise, you would need to install them separately.
Mac User
Step 1) Go to https://www.anaconda.com/download/ and Download Anaconda for Python 3.6 for your OS.
By default, Chrome selects the downloading page of your system. In this tutorial, installation is done for Mac. If you run on Windows or Linux, download Anaconda 5.1 for Windows installer or Anaconda 5.1 for Linux installer.
Step 2) You are now ready to install Anaconda. Double-click on the downloaded file to begin the installation. It is .dmg for mac and .exe for windows. You will be asked to confirm the installation. Click Continue button.
You are redirected to the Anaconda3 Installer.
Step 3) Next window displays the ReadMe. After you are done reading the document, click Continue
Step 4) This window shows the Anaconda End User License Agreement. Click Continue to agree.
Step 5) You are prompted to agree, click Agree to go to the next step.
Step 6) Click Change Install Location to set the location of Anaconda. By default, Anaconda is installed in the user environment: Users/YOURNAME/.
Select the destination by clicking on Install for me only. It means Anaconda will be accessible only to this user.
Step 7) You can install Anaconda now. Click Install to proceed. Anaconda takes around 2.5 GB on your hard drive.
A message box is prompt. You need to confirm by typing your password. Hit Install Software
The installation may take sometimes. It depends on your machine.
Step 8) Anaconda asks you if you want to install Microsoft VSCode. You can ignore it and hit Continue
Step 9) The installation is completed. You can close the window.
You are asked if you want to move Anaconda3
installer to the Trash. Click Move to Trash
You are done with the installation of Anaconda on a macOS system
Windows User
Step 1) Open the downloaded exe and click Next
Step 2) Accept the License Agreement
Step 3) Select Just Me and click Next
Step 4) Select Destination Folder and Click Next
Step 5) Click Install in next Screen
Step 6) Installation will begin
Once done, Anaconda will be installed.
Install R
Mac users
Step 1) Anaconda uses the terminal to install libraries. The terminal is a quick way to install libraries. We need to be sure to point the installation toward the right path. In our case, we set the location of Anaconda to the Users/USERNAME/. We can confirm this by checking anaconda3 folder.
Open Computer and select Users, USERNAME and anaconda3. It confirms that we installed Anaconda on the right path. Now, let's see how macOS write the path. Right-click, and then Get Info
Select the path Where and click Copy
Step 2) For Mac user: