3. Lab Outcomes
1. What is R
1. Invoke the R environment and examine the
R workspace
1. R Basics
4. What is R
▪R is a free open source package
based on the S language developed
by Bell Labs
▪Statistical Programming Language
used to develop statistical software
▪Used by statisticians and data miners
▪Many statistical functions are already
built in
5. Why R
1. Implement statistical
procedures
2. Provide excellent graphics
functionality
3. Excellent start for data analysis
projects
6. Getting Started
◦Where to get R?
◦Go to www.r-project.org
◦Downloads: CRAN
◦Set your Mirror: Anyone in the USA is fine.
◦Select Windows 95 or later.
◦ Select base.
◦ Select R-3.4.3 for Windows
◦ The others are if you are a developer and
wish to change the source code.
8. Objects
◦ Objects should have names
◦ Object Types: vector, matrix … etc.
◦ Object Attributes
◦ mode: numeric, character, boolean
◦ length: number of elements in object
◦ Object Values
◦ assign a value
◦ create a blank object
9. Naming Convention
◦ must start with a letter (A-Z or a-z)
◦ can contain letters, digits (0-9), and/or periods “.”
◦ ex: Var1.1
◦ case-sensitive
◦ mydata different from MyData
◦ do not use underscore “_”
10. Assignment
“<-” used to indicate assignment
▪ > x<-1
▪ > y<-3
▪ > z<-4
▪ > x*y*z
[1] 12
Note: Type determined automatically when variable is
created with "<-" operator
11. Built-In Functions
▪actions can be performed on objects
using functions
▪have arguments and options
▪provide a result
▪parentheses () are used to specify that
a function is being called
14. Objects | Vectors
◦ A series of numbers
◦ Created with:
◦ c() to concatenate elements or sub-vectors
◦ rep() to repeat elements or patterns
◦ seq() or m:n to generate sequences
◦ Example:
◦ X <- c(2,0,0,9)
◦ Y <- seq(2,5) #sequence of integers between 2 & 5
◦ Z <- rep(1,4) #repeat the number 1, 4 times
◦ X+Y+Z
◦ ? *
15. Objects | Accessing Vectors
> x <- c (2,0,0,4)
> x [1] # Select the first element, equivalent to x[c(1)]
[1] 2
x [-1] # Exclude the first element
[1] 0 0 4
> x [1] <- 3 ; x
[1] 3 0 0 4
> x [-1] = 5 ; x
[1] 3 5 5 5
>x<5
[1] TRUE FALSE FALSE FALSE
> x [x<5] = 2 #Edits elements meeting condition
[1] 2 5 5 5
16. Objects | Data Frames
▪ A group or collection of Vectors
▪ Most of the time, when data is loaded, it will be organized in a data frame
Example:
>DF <- data.frame (h=c(150,160), w=c(65,72))
>DF
h w
1 150 65 df[-1,2]
2 160 72
17. Objects | Accessing Data
Frames
> DF[1]
h
1 150
2 160
> DF[1,]
h w
150 65
> DF[2]
w
1 65
2 72
> DF[2,]
h w
160 72
Q1: DF[1,2] ?
Q2:DF[-1,2] ?
18. Hands ON
◦ 1-Create A data Frame with 3 columns :
◦ 1st col = 1,2,3
◦ 2nd col = 10,14,10
◦ 3rd col = 0,0,0
◦ 2-Multiply 1st & 2nd columns and put the result in the 3rd
columns then print the data frame in the same line.