0% found this document useful (0 votes)
13 views

R-programming -Final Lab Manual-2022 (1)

The document outlines various exercises related to operations on matrices and vectors, creation of data frames, and statistical analysis using R programming. It includes programming examples for matrix operations, vector manipulations, data visualization, and hypothesis testing. Each exercise provides a structured approach with algorithms, program code, and expected outputs.

Uploaded by

hitlerkomanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

R-programming -Final Lab Manual-2022 (1)

The document outlines various exercises related to operations on matrices and vectors, creation of data frames, and statistical analysis using R programming. It includes programming examples for matrix operations, vector manipulations, data visualization, and hypothesis testing. Each exercise provides a structured approach with algorithms, program code, and expected outputs.

Uploaded by

hitlerkomanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

EX NO:1 OPERATIONS ON MATRICES

(ADDITION,SUBTRACTION, MULTIPLICATION)

AIM:

ALGORITHM:

Concept : Basic operations on Matrices:


PROGRAM:
mat1.data <- c(1,2,3,4,5,6,7,8,9)
mat1 <- matrix(mat1.data,nrow=3,ncol=3,byrow=TRUE)
mat1

mat2.data <- c(10,11,12,13,14,15,16,17,18)


mat2 <- matrix(mat2.data,nrow=3)
mat2

mat2[1,2] #first row second column of mat2


mat2[c(1,3),c(2,3)] #first and third row and second and third
column
mat2[2, , drop=FALSE] #second row of mat2 with drop=FALSE
mat2[c(-1),c(-2,-3),drop=FALSE] #first row and second and third
column are excluded
mat2[c(TRUE,FALSE,TRUE),c(FALSE,TRUE,TRUE)]
mat2[c(TRUE,FALSE),c(FALSE,TRUE,TRUE)] #the row vector is
recycled to 3 elements.
mat1*5
mat1+2
mat1/2
mat_add <- mat1+mat3
mat_add
mat_sub <- mat1-mat3
mat_sub
mat_mul <- mat1*mat3
mat_mul
OUTPUT:
RESULT:
EX NO:2 OPERATIONS ON VECTORS

AIM:

ALGORITHM:

Concept : Multiplying a vector with a constant


Adding two vectors
Multiplying two vectors
Subtraction
Division operations on vectors
Vectors sequence
Repetition of vectors
PROGRAM:
Vec<-c(1,2,3,4,5)
multivec <- vec*2
multivec
vec2<-c(6,7,8,9,10)
vector_add <- vec+vec2 #vec = 1,2,3,4,5 vec2 = 6,7,8,9,10
vector_add
vector_mul <- vec*vec2
vector_mul
vector_sub <- vec2-vec
vector_sub
vector_div <- multivec/vec # multivec = 2,4,6,8,10
vector_div
vec_seq <- seq(from=1,to=20,length=30)
vec_seq
vec_rep <- rep(c(2,3,4), times=3)
vec_rep
sum(vec_rep)
RESULT:
EX NO:3 Creation of data frames, Bar chart
and Histogram.

AIM:

ALGORITHM:

PROGRAM:
In the drive E create a directory “Sample”
In the note pad type the data shown in k1.csv
setwd("E:\\Sample")
getwd()
sample = read.csv("k1.csv")
data <- read.csv("k1.csv")
head(sample)
# Create a data frame.

# Get the max salary from data frame.


sal <- max(data$salary)
print(sal)
salaries <-(data$salary)
salaries
names<-(data$name)
names
summary(data$salary)
salarybar<-c(names,salaries, byrow=FALSE)

barplot(data$salary,main="SALARY",
ylab='salary',
xlab ="name",
col="blue",
horiz=FALSE,beside=TRUE)
hist(data$salary)
colnames<-c(data$name)
colnames
rowname<-c(data$sa)
OUTPUT:
RESULT:
EX NO:4 Random Number Generator In R.

AIM:

ALGORITHM:

PROGRAM:
df <- data.frame("Serial_number" = 1:5, "Age" = c(20, 21, 17, 23,
19), "Name" = c("raja","ramu", "ravi", "raghu", "rajesh"))
# Sort by age ascending order
newdataAsc <- df[order(df$Age),]
newdataAsc
# sorting is descending order
> newdataDsc> newdataDsc <- df[order(-df$Age),] > newdataAsc
OUTPUT:

RESULT:
EX NO:5 Calculating Mean, Median,
Standard Deviation, Variance For
A Given Set Of Data

AIM:

ALGORITHM:

PROGRAM:
setwd("E:\\Sample")
getwd()
sample2 = read.csv("k5.csv")
data <- read.csv("k5.csv")
data
mean(data$x)
median(data$x)
sd(data$x)
var(data$x)
#mediam absolute variance
mad(data$x)
max(data$x)
min(data$x)
sum(data$x)
length(data$x)

OUTPUT:
RESULT:
EX NO:6 Factorial Using Recursive
Function

AIM:

ALGORITHM:

PROGRAM:
rec_fac <- function(x){
if(x==0 || x==1){
return(1)
} else {
return(x*rec_fac(x-1))
}
}
var = readline();
var = as.integer(var)
print(var)
x<-var
f<-rec_fac(x)
t1<-c("factorial",x,"is",f )
t1

OUTPUT:

RESULT:
EX NO:7 Factorial Using Recursive
Function

AIM:

ALGORITHM:

PROGRAM:
In the drive E create a directory “Sample”
In the note pad type the data shown in k3.csv
(I am attaching the k3.csv file for your kind perusal)

setwd("E:\\Sample")
getwd()
sample1 = read.csv("k3.csv")
data <- read.csv("k3.csv")
head(sample1)
# Create a data frame.

# Get the max salary from data frame.


store_name<-data$store
store_name
car_sales <-data$sales
car_sales
store_advt<-data$advt_cost
store_advt
data<-data.frame(store_name,car_sales,store_advt)
data
scatter.smooth(x=data$car_sales,y=data$store_advt, xlab = "car
sales",ylab = "advertisement cost", main="car sales -
advertisementcost")
Linear_model <- lm(car_sales~store_advt,data = sample1)
print(Linear_model)
summary(Linear_model)
OUTPUT:

RESULT:
EX NO:8 Plot the Graph for Normal
Distribution

AIM:

ALGORITHM:

PROGRAM:
# Create a sequence of numbers between -5 and 5 incrementing it
by 0.2.
x <- seq(-5, 5, by = .1)
# The mean here is 1.0 and standard deviation as 0.
y <- dnorm(x, mean =0, sd = 1)
#Plot the Graph
plot(x,y)
OUTPUT:

RESULT:
EX NO:9 Time Series Graph For A Given Set
Of Data

AIM:

ALGORITHM:

PROGRAM:
sales_ice <- c(450, 613, 466, 205.7,571.0, 622.0, 851.4, 621.4,875.3,
979.7, 927.5,14.45)
sales_cooldrink <- c(550, 713, 566, 687.2,110, 120, 72.4,
814.4,423.5, 98.7, 741.4,345.3)
combined.sales <- matrix(c(sales_ice,sales_cooldrink),nrow = 12)
sales.timeseries <- ts(combined.sales,start = c(2021,1),frequency =
12)
plot(sales.timeseries, main = "Showing ice cream – cooldrink sales")
print(sales.timeseries)
OUTPUT:

RESULT:
EX NO:10 Hypothesis Testing Using R
Programming

AIM:

ALGORITHM:

PROGRAM:
x <- sample(c(1:100),size=20,replace=TRUE)
y <- sample(c(1:100),size=20,replace=TRUE)
t1<- c("value of variable x")
t1
x
t2<- c("value of variable y")
y
t3<-c("t test ")
t3
t.test(x,y)
t4<-c("Correlation between x and y")
t4
cor(x,y)
t5<-c("covarience")
t5
cov(x,y)

OUTPUT:
RESULT:

You might also like