1. Introduction to R
a. What is R?
R is an open source programming language designed for statistical computing & graphics.
- Free to use
- Reproducible
- Community support
- Great libraries
- Diverse industries (health, academia to business sector)
Note: Whatever you’re trying to do, you’re probably not the first to try doing it R. Chances are good that someone has already written a package for that.
b. Creating a New file
- Go to File > R Script
2. Basics of R
a. R as Calculator
## [1] 15
## [1] 10
## [1] 50
## [1] 20
b. Function
A function performs a specific task when called.
Types of Function:
Types | Functions |
---|---|
Built-in | print(), sum(), mean() |
User-defined | eg: add.me() |
## [1] "My name is Binod"
## [1] 5050
## [1] 5
## [1] 10
## [1] 10
## [1] 4
## [1] 3
c. Assignment
- Process to store values
- The <- operator is used for assignment.
- We assign values in a variable.
## [1] 1275
## [1] 5
d. Mutiplication Table
## Type a number:
## [1] "NA x 1 = NA"
## [1] "NA x 2 = NA"
## [1] "NA x 3 = NA"
## [1] "NA x 4 = NA"
## [1] "NA x 5 = NA"
## [1] "NA x 6 = NA"
## [1] "NA x 7 = NA"
## [1] "NA x 8 = NA"
## [1] "NA x 9 = NA"
## [1] "NA x 10 = NA"
3. Data Structures in R
To make best out of R, you need solid basics of data structures and how to operate on those.
To understand computations in R, two slogans are helpful:
- Everything that exists is an object.
- Everything that happens is a function call.
Dimension | Type | |
---|---|---|
- | Homogeneous | Heterogeneous |
1D | Vector | List |
2D | Matrix | Data Frame |
nD | Array* | - |
Note: Homogeneous allow only single data types.
3.1 Vector
Vector is one-directional sequence of elements having same data types.
Type | Example |
---|---|
Logical | TRUE, FALSE |
Integer | 2L, 5L (L defines as integer) |
Numeric | 2, 5.5 (Double i.e ‘real number’) |
Complex | 1+3i |
Character | “apple”, “ball” |
R provides many functions to examine features of vectors and other objects, for example
Function | Description |
---|---|
class() | what kind of object is it (high-level)? |
typeof() | what is the object’s data type (low-level)? |
length() | how long is it? What about two dimensional objects? |
attributes() | does it have any metadata? |
b. Making different types of atomic vectors
student.name <- c("Anu", "Bikash", "Saroj")
student.age <- c(20, 24, 27)
student.gender <- c("Female", "Male", "Male")
student.married <- c(FALSE, FALSE, TRUE)
student.id <- 1:length(student.name)
Type checking:
## [1] "double"
## [1] "numeric"
3.2 List
A list is sequence of elements having same or different data types.
a. Creating a list
b. Create a list
## [1] 10
## [1] "list"
c. Creating student’s list
student <- list(id = student.id,
name = student.name,
age = student.age,
gender = student.gender,
marital.status = student.married)
student
## $id
## [1] 1 2 3
##
## $name
## [1] "Anu" "Bikash" "Saroj"
##
## $age
## [1] 20 24 27
##
## $gender
## [1] "Female" "Male" "Male"
##
## $marital.status
## [1] FALSE FALSE TRUE
d. Checking the list elements
## $name
## [1] "Anu" "Bikash" "Saroj"
## [1] 20 24 27
3.3 Matrix
Matrix is a collection of data elements arranged in a two-dimensional rectangular layout.
a. Creating a matrix
b. Create two vectors x and y
c. Combine two vectors by row or col
3.4 Data Frame
Data frame is a list of vector having equal length and used for storing data tables.
a. Creating data frame
c. Useful functions for Data Frame
Functions | Description |
---|---|
head() | see first 6 rows |
tail() | see last 6 rows |
dim() | see dimensions |
nrow() | number of rows |
ncol() | number of columns |
str() | structure of each column |
names() | will list the names attribute for a data frame (or any object really), which gives the column names. |