R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

R data types

numeric

a <- 1 
class(a)
## [1] "numeric"
str(a)
##  num 1

character

b <- "hello"
class(b)
## [1] "character"

factor

b.fact <- as.factor(b)
class(b.fact)
## [1] "factor"

logical

a <- 5
b <- 5
c <- 6
a == b
## [1] TRUE
a == c
## [1] FALSE
logical.1 <- a == b
class(a == b)
## [1] "logical"
class(logical.1)
## [1] "logical"

Object

in R, you can assign any object with anything - in this example we are going to use ‘murders’ dataset which is saved in the ‘dslabs’ package - We install a package in R by the function ‘install.packages’ from R base - A package can contain functions, datasets, even apps like ‘ShaheenClinic’ package - A function in R can do a mathematical operation, logical operation and contains some arguments between the brackets ()

#install.packages("dslabs")
# you can remove a package by the funtion: remove.packages("dslabs")

library(dslabs)
data(murders)

assign data.frame to the object

d <- murders

accessor sign $

to access a variable / column in a data.frame you have to use the accessor sign $

murders$region
##  [1] South         West          West          South         West         
##  [6] West          Northeast     South         South         South        
## [11] South         West          West          North Central North Central
## [16] North Central North Central South         South         Northeast    
## [21] South         Northeast     North Central North Central South        
## [26] North Central West          North Central West          Northeast    
## [31] Northeast     West          Northeast     South         North Central
## [36] North Central South         West          Northeast     Northeast    
## [41] South         North Central South         South         West         
## [46] Northeast     South         West          South         North Central
## [51] West         
## Levels: Northeast South North Central West

That’s all for now, see you in the coming lectures Enjoy your time