Illya Mowerman
11/29/2017
Vectors can be character or numeric
char_vector <- c('Hola' , 'me' , 'llamo' , 'Illya')
num_vector <- c(1, 4 , 6 , 7)
print(char_vector)
## [1] "Hola" "me" "llamo" "Illya"
print(num_vector)
## [1] 1 4 6 7
str(char_vector)
## chr [1:4] "Hola" "me" "llamo" "Illya"
str(num_vector)
## num [1:4] 1 4 6 7
summary(num_vector)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 3.25 5.00 4.50 6.25 7.00
There are several data frames already in R. Letās use the mtcars data. The mtcars data frame is to R what Fisherās Iris data is to statistics.
The head function allows us to see the first six records.
You try it
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
The summary function gives us some basic statistics
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
mean(mtcars$mpg)
## [1] 20.09062
We can render graphs directly into the plots pane, or sink them to a .png file.
ggplot(mtcars) +
geom_point (aes(mpg , wt))
We can also store graphs in an object.
my_first_plot <- ggplot(mtcars) +
geom_smooth(aes(mpg , wt))
The reason for storing a graph in an object is to be able to use it at a later time.
my_first_plot
## `geom_smooth()` using method = 'loess'
Functions in R work like basic mathematical functions. Below is a function for calculating the area of a circumference.
\(f(r) = \pi*r^{2}\)
circ_area <- function(diameter){
pi*(diameter/2)^2
}
circ_area(3)
## [1] 7.068583
Now you try creating the same function and calculate the are of a circumference of diameter 5
circ_area <- function(diameter){
pi*(diameter/2)^2
}
circ_area(3)
## [1] 7.068583
Note that although you can write an infinite amount of code within a function, functions can only return one object.
We can get around that by returning a list.
Think of lists as a box where you can put any and all objects.
Below is a version of Noahās Singles only Arc of list
# defining my list
noahs_singles_arc <- list()
# inserting objects
noahs_singles_arc$char_vector <- char_vector
noahs_singles_arc$mtcars <- mtcars
noahs_singles_arc$my_first_plot <- my_first_plot
noahs_singles_arc$circ_area <- circ_area
Letās take a quick look at the list
summary(noahs_singles_arc)
## Length Class Mode
## char_vector 4 -none- character
## mtcars 11 data.frame list
## my_first_plot 9 gg list
## circ_area 1 -none- function
Packages are functions that are created by the R community and have made them available to all
Installing packages is easy (the repos part is not necessary):
install.packages('tidyverse' , repos='http://cran.us.r-project.org')
##
## The downloaded binary packages are in
## /var/folders/8c/w4htphd93r7cq7tcyp65xr780000gn/T//RtmpchaG6y/downloaded_packages
To be able to use the functions of a package, you must load it first
library(tidyverse)
To get help on a specific function simply put a ? in front of the function, and the help facility will display the documentation. Note that it is extremely useful to scroll down to the examples
?sum
Go to the following website to download the data:
https://www.ibm.com/communities/analytics/watson-analytics-blog/hr-employee-attrition/
This link takes you directly to the data:
https://community.watsonanalytics.com/wp-content/uploads/2015/03/WA_Fn-UseC_-HR-Employee-Attrition.xlsx
install.packages('readxl')
library(readxl)