Lab Manual Page No 1
Lab Manual Page No 1
Lab Manual Page No 1
Windows Users need to first download and install binaries for base distribution.The current version of
base binary distribution is R 3.3.1. Users Can Check and download Previous Contributions and
versions of R .R tools from the mirror website R tools is used for building R and it’s Packages.
Installing R on windows is Simple Users need to double click the downloaded binary named R-3.3.1 –
win- exe ,On a graphical interface command line installation options are available for windows.
Command .libpaths( ) can be used to get or Set the path of the package library.
>.libpaths( )
O/P: C:/R/R-3.4.3/library.
2. R data types
Int,char ,float,double , Boolean ,complex,raw are the basic data types in R
x=5.6
print(class(x))
print(typeof(x))
y=5
print(class(y))
print(typeof(y))
x=as.integer(5)
print(class(x))
print(typeof(x))
y=sL
y=5L
print(class(y))
print(typeof(y))
x=4
y=3
z=x>y
print(z)
print(class(z))
print(typeof())
print(typeof(z))
x=4+3i
print(class(x))
print(typeof(x))
char="Magnet"
print(class(char))
print(typeof(char))
x=as.integer(6)
print(class(x))
raw_variable <-charToRaw("welcome to Programiz")
print(raw_variable)
print(class(raw_variable))
char_variable <-rawTochar(raw_variable)
char_variable <-rawToChar(raw_variable)
print(char_variable)
print(class(char_variable))
dbl_var <- c
dbl_var <- c(1L,2.5,4.5)
dbl_var
int_var <- c(1L,6L,10L)
int_var
3. Program make a simple calculator that can add, subtract, multiply and divide using functions
add <- function(x, y) {
return(x + y)
return(x - y)
return(x * y)
return(x / y)
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
Output
[1] "1.Add"
[1] "2.Subtract"
[1] "3.Multiply"
[1] "4.Divide"
Enter choice[1/2/3/4]: 4
RectangleWidth <- 4
RectangleHeight
[1]2
RectangleWidth
[1] 4
RectangleArea
[1] 8.
SampleSuperstore
library( )
library(tools)
library( )
library(Matrix)
data( )
iris
data(iris)
Orange
Rcurl
WDI
getURL
getURL( )
htmlTreeParse()
install.packages("rjson")
Rcurl
library(Rcurl)
library(Rcurl)
RCurl
library(RCurl)
RCurl
library(RCurl)
RCurl
library()
library()
data()
library()
library()
lirary()
library()
View(mtcars)
ncol(mtcars)
View(mtcars)
.libPaths()
Installed.Packages()
packageDescription("stats")
help(package="stats"
help(package="stats")
help(package="stats")
packageDescription("matrix package")
packageDescription("matrix")
plot(tress,col="red",pch=33)
.libPaths()
installed.Packages()
packageDescription("matrix")
help(package="matrix")
help(package="datsets")
datasets::air passengers
datasets::Air passengers
datasets::AirPassengers
library(datasets)
AirPassengers
.libpaths()
.libPaths()
installed.Packages()
installed.packages()
packageDescription("parellel")
packageDescription("Parellel")
packageDescription("Parellel")
packageDescription("tools")
packageDescription("utils")
packageDescription("translations")
packageDescription("servivel")
packageDescription("servival")
packageDescription("survival")
find.packages("survival")
find.Packages("survival")
data()
data
data(trees)
trees
head(trees,n=7)
tail(trees,n=2)
summary(trees)
View(trees)
trees
edit(trees)
edit(trees)
edit(trees)
edit(trees)
plot(trees)
edit(trees)
plot(trees,color)
plot(trees,col="green")
dir()
list.files()
plot(tress)
plot (tress)
plot(tress)
plot(trees,col="green")
plot(trees,col="green")
plot(trees,col="blue")
plot(trees,col="pink")
plot(trees,col="red")
read.csv()
read.xlsx()
mtcars
summary(mtcars$mpg)
str(c(1,2,3,4,5,6))
10.Arthamatic expressions in R
9+23
4-2
5*4
%/4
5/4
4^5
4**5
23 %%9
5%/%4
sqrt(9)
sqrt(225)
2<4
T==FALSE
F==FALSE
x <- c(1:5)
x
x[(x>2)|(x<5)]
x[x>2)&(x<5)]
x[(x>2)&(x<5)]
x>2
x<4
x==3
x>=3
x<=2
install of rjson xml packages in R
install.packages("rjson")
install.packages("XML")
Titanic()
Titanic ()
Titanic()
titanic()
OPERATIONS ON SEQUENCE
a <- seq(5,11,by=2)
a
11.MATRIX IN R
matrix(a,2,2)
matrix(a,2,1)
matrix(a,2,2)
matrix(a,1,2)
dim(a) <- c(1,2)
diam(a) <- c(2,2)
dim(a) <- c(2,2)
a
x <- 6:11
x
mat <- matrix(x,2,3)
mat
mat <- matrix(x,3,2)
mat
mat <- matrix(x,3,3)
mat
mat [2,2]
mat [3,3]
mat [2,3]
mat [1,1]
mat [3,2]
mat [3,1]
mat [2,1]
mat [2,3]
mat[,3]
mat[2,]
mat[3,]
sin ,cos functions in R
sin(x)
cos(x)
tan(x)
sin(90)
cos(90)
tan(90)
cot(90)
c(1,2,6)+c(11,6,55)
x <- seq(1,20,0.1)
x
y <- sin(x)
y
plot(x,y)
x <- seq(1,20,5)
x
y <- cos(x)
y
plot(x,y)
x <- seq(1,2,0.1)
x
x <- seq(0,1,0.1)
x
y <- cos(x)
y
plot(x,y)
x <- seq(1,20,0.1)
x
y <-seq(x)
y
y <- cos(x)
y
plot(x.y)
plot(x,y)
reading .csv file in R
read.csv("sampledata.csv")
InputData <- read.csv("D:/samledata.csv")
read.csv('D:/Sampledata.csv')
read.table('D:/sampladata.csv',header=TRUE,sep= ',',)
read.table('D:/sampladata.csv',header=TRUE)
read.table('D:/sampledata.csv')
read.table('D:/sampladat.csv',header=FALSE)
read.table('D:/sampledata.csv', header=TRUE)
read.table('D:/sampledata.csv', header=TRUE, sep=',', )
read.table('D:/sampledata.csv', header=TRUE, sep=',',....)
read.csv('D:/sampledat.csv')
read.csv('D:/sampledata.csv')
Inputdata <- read.csv("D:/sampledata.csv")
save.image("C:\\Anitha\\R.R.Rao 19-12-22")
q()
data()
x <- c(v,s,p,a,l)
x <- c(a,p by=2)
x <- seq(10,25,by=5)
x
matrix(a,2,2)
dim(a)
edix(x)
edit(x)
12.Descriptive Statistics in R
mtcars
summary(mtcars)
min(mtcars)
max(mtcars)
range(mtcars)
mean(mtcars)
BOD
mean(BOD)
IQR(mtcars)
mtcars
IQR(mtcars)
x <-1:6
x
summary(x)
min(x)
max(x)
range(x)
mean(x)
median(x)
mad(x)
IQR(x)
quantile(x)
IHR(x)
apply(x,1,mean)
matrix(x)
matrix(x,3,2)
dim(x)
apply(x,1,mean)
a <- seq(10,25,by=5)
a
matrix(a,2,2)
q()
x<- c(6,11,4)
median.result <- medain(x)
median.result <- median(x)
print(median.result)
x<- c(-6,11,4)
median.result <- median(x)
print(median.result)
numbers <- c(6,11,4)
median(numbers)
barplot(numbers)
abline(h = medain (numbers))
abline(h= median (numbers))
q()
numbers <- c(6,11,4)
mean(numbers)
deviation <- sd(numbers)
deviation
barplot,abline functions in R
barplot(numbers)
abline(h= sd(numbers))
abline(h= sd(numbers)+ mean(numbers))
11.Apply,bins,median,sd,histogram ,barplot,abline functions in R
mtcars
summary(mtcars)
min(mtcars)
max(mtcars)
range(mtcars)
mean(mtcars)
BOD
mean(BOD)
IQR(mtcars)
mtcars
IQR(mtcars)
x <-1:6
x
summary(x)
min(x)
max(x)
range(x)
mean(x)
median(x)
mad(x)
IQR(x)
quantile(x)
IHR(x)
apply(x,1,mean)
matrix(x)
matrix(x,3,2)
dim(x)
apply(x,1,mean)
a <- seq(10,25,by=5)
a
matrix(a,2,2)
q()
x<- c(6,11,4)
median.result <- medain(x)
median.result <- median(x)
print(median.result)
x<- c(-6,11,4)
median.result <- median(x)
print(median.result)
numbers <- c(6,11,4)
median(numbers)
barplot(numbers)
abline(h = medain (numbers))
abline(h= median (numbers))
q()
numbers <- c(6,11,4)
mean(numbers)
deviation <- sd(numbers)
deviation
barplot(numbers)
abline(h= sd(numbers))
abline(h= sd(numbers)+ mean(numbers))
q()
h <- c(1,2,3)
bins <- c(0,5,10,15)
bins
hist(h,xlab="Values",ylab="Colours",col="red",xlim=c(0,3),
ylim=c(0,3),breaks=bins)
EmpNo <- c(6,11,1966)
EmpName <- c("RAM","RAHIM","ROBERT")
ProjName <- c("R","PL","GBMKL")
Employee <- data.frame(EmpNo, EmpName, ProjName)
Employee
Employee[2]
Emp[1:2]
Employee[1:2]
Employee[3,]
Employee[,3]
row.names(Employee) <- c("Employee1","Employee2","Employee3")
row.names(Employee)
Employee
Employee["Employee1"]
Employee["Employee1",]
Employee["Employee3",]
Employee[c ("Employee 2", "Employee 1"),]
Employee [ c ("Employee 2","Employee 1")]
Employee [ c("Employee 1","Employee 2"),]
Employee
Employee [ c ("Employee 1", "Employee 2"),]
Employee [ "Employee2",]
Employee[["EmpName"]]
Employee[c("EmpNo", ProjName")]
Employee[ c("EmpNo", "ProjName")]
Employee
Employee$EmpExpYears <- c(6,11,4)
Employee
Employee[order(Employee$EmpExpYears),]
Employee[order(-Employee$EmpExpYears),]
dim(Employee)
nrow(Employee0
)
nrow(Employee)
names(Employee)
edit(Employee)
Employee[1:3,]
Employee[1:3,1:2]
head()
head(Employee)
subset(Employee, EmpExpYears >6)
subset(Employee, EmpExpYears >4 ,select = c(EmpNo))
subset(Employee, Employee=="keir")
subset(Employee, EmpName=="keir")
subset(Employee, EmpName == "keir")
Employee
subset(Employee, EmpNo == "6")
subset(Employee, EmpNo == "6" | EmpNo == "11")
reading txt file in R
read.table("d:/sep,txt", sep="\t")
read.table("d:/sep.txt", sep="\t")
read.table("d:/sep.txt", sep="\t",header=TRUE)
mydata = read.table("d:/mydata.txt")
mydata = read.table("d:/mydata.txt")
mydata = read.table("d:/mydata.txt")
mydata = read.table("c:/mydata.txt")
v <- c(5,6,4,8,5,7,4,6,5,8,3,2,1)
resultmode <- getmode(v)
print(resultmode)
charv <- c("Rt","cc","cc","bm","cc")
resultmode <- getmode (charv)
print(resultmode)
save.image("C:\\Users\\R\\OneDrive\\Documents\\.RData")
x <- c(15,54,6.5,9.2,36,5.3,8,-7,-5)
result.mean <- mean(x)
print(result.mean)
x <- c(1,2,3,4,5,6)
result.mean <- mean(x)
print(result.mean)
result.mean <- mean(x,trim= 0.1)
print(result.mean)
x <- c(6,11,4,NA)
result.mean <- mean(x)
print(result.mean)
result.mean <- mean(x,na.rm= TRUE)
print(result.mean)
numbers <- c(6,11,4)
mean(numbers)
barplot(numbers)
abline(h = mean (numbers))
barplot(numbers)
abline(h = mean (numbers))
q()
h <- density(c(6,11,4))
plot(h)
plot(h,xlab="Values",ylab="Density")
plot(h,xlab="Values",ylab="Density")
h <- c(6,11,4)
y <- C(80,90,100)
print(summary(relation))
PREDICT( ) function in R
a <- data.frame(x=170)
print(result)
[1]
85
abline(lm(x~y)), cex=1.3, pch = 16, xlab = “ Weight in Kg”, ylab = “height in cm”)
Cars
head(Cars)
beta co-efficients in R
print(linear Mod)
print(Summary(linear Mod))
plot(Cars $dist, Cars $Speed, col = “red”, main = “Speed & Distance Regression “ ,
abline (lm(Cars$Speed ~ Cars$ dist)), cex = 1.3, pch = 16, xlab = “Distance”, ylab =
“Speed”)
res
[1,] 6 9
[2,] 5 10
[3,] 10 0
EN = (E == “N”)
EC = (E == “C”)
b1
summary(b1)
f <- function(x)
Sum ((x-1)^2)
summary(m)
optim( ) function in R
Sum((x-1)^2)
Optim(c(10,10),f)
Sum((n-1)^2)
nlm(f, c(10,10))
Tree Models
Install.packages(“tree”)
library(tree)
attach (Cars)
names(Cars)
plot(model)
text(model)
22. autocorrelation function, partial autocorrelation implemention in R
Z <- rnorm(250,0,2)
Y[1]-Z[1]
plot.ts(Y)
acf(Y)
Y <- numeric(250)
Y[1]-Z[1]
plot.ts(Y)
acf(Y)
Y <- rnorm(250,0,2)
par(mfrow = c(1,2))
plot.ts(Y)
acf(Y)
par(mfrow = c(1,2))
acf(x,type = “p”)
acf(y,type = “p”)
par(mfrow=c(1,1))
acf(cbind(x,y))
acf(cbind(x,y),type = “p”)
23.detrended function in R programming
y <- numeric(length(x)-2)
ts.plot(detrended)
par(mfrow= c(1,2))
acf(Lynx,main=””)
acf(Lynx,type = “p”,main=””)
AIC(model10,model20)
R <- 1:16
mat
m <- mat
dist(m,method = “euclidean”)
dist(m,method =”Canberra”)
mtcars
ncol(mtcars)
[1] 11
nrow(mtcars)
[1] 32
rbind(x,y)
dist(rbind (x,y))
dist(rbind(y,z))
dist(as.matrix(mtcars))
26. hcluster in R
h1 <- hclust(ed)
h1.
# plotting of clustering
Plot(h1)
# Creating a matrix
m <- matrix[1:20,4,4]
Km <- kmeans(m,centers=3)
Km
Plot(m,col = (km$cluster),
pch= 20,cex= 4)
iris
head(iris)
head(newiris)
km <- kmeans(newiris,3))
table(iris$Species,km$cluster)
28.CURE algorithm
Algorithm.
R <- 1:16
[1] 1 2 3 4 5 6…………16
mat
m <- mat
# Manhattan distance
# maximum distance
# Canberra distance
# minkowsi distance
apriori ( ) function
The Package a rules provides a function apriori ( ) that performs association rule mining using
Apriori algorithm .The function determines the frequent itemsets ,association rules and
association hyperedges.
eclat( ) function
Support(x,transcations,type,…)
ri <- ruleInduction(ap,TM)
ri
Inspect(head(ri,by =”lift”)
Sample(x,size,replace,…..)
random transactions(nItems,nTrans,method,….)