R Crash Course For Business
R Crash Course For Business
R Crash Course For Business
Crash Course
Kelley 303
Technology and Business Analyst
1
Outline
Part 1 – Introduction to R and RStudio
Part 2 – Working with data
Part 3 – R Graphics
Part 4 - Deployment, Sharing, and Publishing with R
2
Rstudio Basics
3
Getting Started
4
Getting Started
http://www.rstudio.com/products/rstudio/download/ 5
Rstudio Window
6
Rstudio Screen
7
Open New File
9
Editor – “Source/Script”
10
Console– “Command Line”
11
History and Objects
12
Extremely Useful Panel
13
Files
The files panel gives you access to the file directory on your hard drive
14
Files for Workshop
15
Telling R Where Your Files Are
Select Session > Set Working Directory > Choose Directory >
Select CrashCourse folder in Desktop > Open
16
Packages - Useful Libraries
1. Shows a list of all the R packages installed
2. Indicates whether the package is currently loaded
17
Packages
18
Select Package Content - Stats
19
Scroll Stats Content
20
Help!
1. TYPE IN CONSOLE:
?hist
2. CLICK ENTER
21
Help!
24
RUN versus ENTER: Editor versus Console
4. CLICK RUN
5. RESULTS - CONSOLE
26
Packages
Source: https://bookdown.org/ndphillips/YaRrr/packages.html 27
Installing Packages Practice - Wordcloud
28
Exploring Data
29
Load CSV into R
pirates = read.csv(“pirates.csv”)
You can use single quotes or double quotes for file names
30
Data Storage in R
31
View Entire Dataset
Click on pirates
32
Take a Closer Look at Your Data
33
Descriptive Statistics: Min, Max, Mean, Table
mean(pirates$age)
max(pirates$height) Continuous variable
table(pirates$sex)
pirates$age
Table name $ Column name
Categorical variable
34
Summary of Stats Functions
practice
each
function:
Type in
your
editor
examples
and RUN
35
Aggregate Function
Names of columns
Name of dataset
Type of Statistics
Change mean to sum: aggregate(formula = age ~ sex, data = pirates, FUN = sum)
36
Plotting
You can select and highlight two lines, then click RUN. Or RUN each line at a time
If plot does not fit your window, adjust the Rstudio layout and RUN plot again
Error in plot.new() : figure margins too large
37
Saving Plot
38
Adding Title, Labels, and Color
39
Adding Regression Line
40
Box Plot
boxplot
41
How to Interpret Box Plot
42
Source: https://www.wellbeingatschool.org.nz/information-sheet/understanding-and-interpreting-box-plots
How to Interpret Box Plot
43
Source: https://www.wellbeingatschool.org.nz/information-sheet/understanding-and-interpreting-box-plots
Hypothesis testing
44
T-test – two sample
Difference between two variables
45
Source: https://stats.idre.ucla.edu/other/mult-pkg/faq/general/faq-what-are-the-differences-between-one-tailed-and-two-tailed-tests/
T-test – two sample
Difference between two variables
46
Correlation Testing
Correlation between two variables
47
Linear Regression Model
Can we use pirate’s age, weight, and number of tattoos
to predict how many treasure chests they found?
DV – dependent variable
IV – independent variable
48
Time Series
49
Import EU Stocks Data 1991-1998
eu <- read.csv("eustockmarkets.csv")
head(eu)
50
Create Time Series EU Stocks 1991-1998
start(time_series)
end(time_series)
51
Plot Time Series EU Stocks 1991-1998
plot(time_series)
52
Time Series Plot EU Stocks 1991-1998
53
Add Legend - Time Series Plot EU Stocks 1991-1998
ts.plot(time_series, col = 1:4, xlab = "Year", ylab = "Index Value", main
= " Major European Stock Indices, 1991-1998 ")
54
Debugging
55
Debugging R
Console:
Source: https://bookdown.org/ndphillips/YaRrr/debugging.html 57
Debugging R
Source: https://bookdown.org/ndphillips/YaRrr/debugging.html 58
Debugging R
Console:
Source: https://bookdown.org/ndphillips/YaRrr/debugging.html 59
Debugging R
Console:
Source: https://bookdown.org/ndphillips/YaRrr/debugging.html 60
Syntax
61
R Syntax
62
Strings
63
Strings: Practice
64
Vector
65
Length
length() – a function
66