R Programming Materials
R Programming Materials
R as a calculator
> log2(32)
[1] 5
> sqrt(2)
[1] 1.414214
> 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
[,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")
> 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
}
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
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)
> 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)
> 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