Introduction to R and R Studio
R as a calculator
> log2(32)
[1] 5
> sqrt(2)
[1] 1.414214
> seq(0, 5, length=6)
[1] 0 1 2 3 4 5
R as a calculator
> 2+2
[1] 4
> 2+2^2
[1] 6
> (2+2)^2
[1] 16
> sqrt(2)
[1] 1.414214
> log(2)
[1] 0.6931472
>x=5
> y = 10
> z <- x+y
>z
[1] 15
R as a calculator
> seq(1,5, by=.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5
4.0 4.5 5.0
> v1 = c(6,5,4,3,2,1)
> v1
[1] 6 5 4 3 2 1
> v2 = c(10,9,8,7,6,5)
> v3 = v1 + v2
> v3
[1] 16 14 12 10 8 6
R as a calculator
> v3
[1] 16 14 12 10 8 6
> max(v3);min(v3)
[1] 16
[1] 6
> length(v3)
[1] 6
> mean(v3)
[1] 11
variables
Numeric
> a = 49
> sqrt(a)
[1] 7
Character
string
> a = "The dog ate my homework"
> sub("dog","cat",a)
[1] "The cat ate my homework“
Logical
> a = (1+1==3)
>a
[1] FALSE
vectors
> a = c(1,2,3)
> a*2
[1] 2 4 6
Lists
> myList<-list(5,6,"seven")
> myList
[[1]]
[1] 5
[[2]]
[1] 6
[[3]]
[1] "seven"
Lists
> d = list(name=“John",age=28,married=F)
> d$name
[1] “John“
> d$age
[1] 28
Lists
Create lists:
list1 <- list(1:5)
print(list1)
list2 <-list(10:14)
print(list2)
Convert the lists to vectors:
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)print(v2)
Now add the vectors:
result <- v1+v2
print(result)
Matrices and Arrays
>x <- matrix(1:6,ncol=3) # Equivalently, enter matrix(1:6,nrow=2)
>x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> x1 <- matrix(1:12,ncol=4)
> x1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
Matrices and Arrays
x1[2:3,c(1,4)] # Extract rows 2 & 3 & columns
1&4
x1[2,] # Extract the second row
x1[-2,] # Extract all rows except the second
x1[-2,-3] # Extract the matrix obtained by
omitting row 2 & column 3
Matrix Addition & Subtraction
Create two 2x3 matrices:
>matrix1 <- matrix(c(3, 9, -1, 4, 2, 6), nrow = 2)
>print(matrix1)
> matrix2 <- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
>print(matrix2)
Add the matrices:
>result <- matrix1 + matrix2
>cat("Result of addition","\n")
>print(result)
Subtract the matrices:
>result <- matrix1 - matrix2
>cat("Result of subtraction","\n")
>print(result)
Matrix Multiplication & Division
Multiply the matrices:
result <- matrix1 * matrix2
cat("Result of multiplication","\n")
print(result)
Divide the matrices:
result <- matrix1 / matrix2
cat("Result of division","\n")
print(result)
Arrays
x <- 1:24 Then
dim(x) <- c(2,12) turns this into a 2 x 12 matrix.
>x
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 3 5 7 9 11 13 15 17 19 21 23
[2,] 2 4 6 8 10 12 14 16 18 20 22 24
Arrays
>dim(x) <-c(3,4,2)
>x
,,1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
,,2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
Examples
> mat<-matrix(c(2,3,1,5),nrow=2,ncol=2)
> mat
[,1] [,2]
[1,] 2 1
[2,] 3 5
Examples
> m<-matrix(1,nrow=2,ncol=3)
>m
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
Naming Columns and Rows
Create two vectors of different lengths:
>vector1 <- c(5,9,3)
>vector2 <- c(10,11,12,13,14,15)
>column.names <- c("COL1","COL2","COL3")
>row.names <- c("ROW1","ROW2","ROW3")
>matrix.names <- c("Matrix1","Matrix2")
Take these vectors as input to the array:
>result <- array(c(vector1,vector2),dim = c(3,3,2),dimnames =
list(column.names,row.names, matrix.names))
>print(result)
Examples
> matrix(c(1,2,3,4,5,6,7),ncol=3)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 1
[3,] 3 6 2
**Warning message:
Replacement length not a multiple of the
elements to replace in matrix(...)
Working with Data Frames
> x<-c(1,3,2,1)
> y<-c(2,3,4,1)
> xy<-data.frame(x,y)
> xy
x y
1 1 2
2 3 3
3 2 4
4 1 1
Working with Data Frames
#use q to create new vector extracting x
column of data frame xy
> q<-xy$x
>q
[1] 1 3 2 1
Working with Data Frames
To address a specific element of a data frame,
address that vector with the appropriate index:
> xy$x[2]
[1] 3
Branching
if (logical expression) {
statements
} else {
alternative statements
}
else branch is optional
Loops
for(i in 1:10) {
print(i*i)
}
i=1
while(i<=10) {
print(i*i)
i=i+sqrt(i)
}
Syntax of if…else statement
if (test_expression)
{
statement1
}
else
{
Statement2
}
Example of if…else statement
x <- -5
if(x > 0)
{
print("Non-negative number")
}
else
{
print("Negative number")
}
Output
[1] "Negative number"
Example of if…else statement
>x<-5
>if(x > 0) print("Non-negative number") else
print("Negative number")
Output
“Non-negative number”
Example of if…else statement
>x <- -5
>y <- if(x > 0) 5 else 6
>y
[1] 6
Syntax of if…else if .. else statement
if ( test_expression1)
{
statement1
}
else if ( test_expression2)
{
Statement2
}
else if ( test_expression3)
{
Statement3
}
else
statement4
Examples
x <- 0
if (x < 0)
{
print("Negative number")
}
else if (x > 0)
{
print("Positive number")
}
else
print("Zero")
Output
[1] "Zero"
Syntax of for loop
for (val in sequence)
{ statement }
Example of for loop
x <- c(2,5,3,9,8,11,6)
count <- 0
for (val in x)
{
if(val %% 2 == 0)
count = count+1
}
print(count)
Syntax of while loop
while(cond)
{
expr
}
cond: condition
expr: expression
Example of while loop
x <- 1
while(x < 5)
{
x <- x+1;
print(x);
}
[1] 2
[1] 3
[1] 4
Next and Break statement
Let's break the loop when x=3:
x <- 1
while(x < 5)
{
x <- x+1;
if (x == 3)
break;
print(x);
}
[1] 2
Factorial by using R
s=1
n=5
for(i in 1:n)
{
s=s*i
}
Print(s)
R - CSV Files,
CSV(Comma Separated Values)
We can create this file using windows notepad by
copying and pasting this data. Save the file
as input.csv using the save As All files(*.*)
option in notepad.
Reading a CSV File
>data <- read.csv("input.csv")
>print(data)
Analyzing the CSV File
> print(ncol(data))
> print(nrow(data))
Analyzing the CSV File
> sal<-max(data$salary)
> print(sal)
> fetch<-subset(data,salary==max(salary))
> print(fetch)
Analyzing the CSV File
> fetch<-subset(data,dept=="IT")
>print(fetch)
> fetch<-subset(data,dept=="IT" & salary>600)
> print(fetch)
Writing into a CSV File
> m<-read.csv(file="c:/as.csv")
> fetch<-subset(m,salary>100)
>print(fetch)
> write.csv(fetch,"output.csv")
> n<-read.csv("output.csv")
> print(n)
Reading a Excel File
• readxl package
• read_excel
• read_xlsx
Download data directly from internet
by using R
• x<-c
("https://raw.github.com/vincentarelbundock/Rdatas
ets/master/csv/datasets/Titanic.csv")
• download.file(url=x,destfile="Titanic.data")
• a<-read.csv("titanic.data",sep=',',header=FALSE)
• View(a)
Linear Regression,
One Example
Values of height:
151, 174, 138, 186, 128, 136, 179, 163, 152, 131
Values of weight:
63, 81, 56, 91, 47, 57, 76, 72, 62, 48
Linear Regression
>x <- c(151, 174, 138, 186, 128, 136, 179, 163,
152, 131)
>y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
>relation <- lm(y~x)
>print(relation)
> print(summary(relation))
Linear Regression
Find weight of a person with height 170
>a <- data.frame(x = 170)
>result <- predict(relation,a)
>print(result)
R Packages
• sqldf
References
• Michael J Crawley, The R Book, Wiley,
http://www.ievbras.ru/ecostat/Kiril/R/Biblio_N
/R_Eng/Crawley2013.pdf
• Mark Gardener, Beginning R The Statistical
Programming Language, Wiley,
https://students.aiu.edu/submissions/profiles/re
sources/onlineBook/A7E7d8_Beginning%20R
%20statistics.pdf
Thank You