0% found this document useful (0 votes)
9 views16 pages

R Programming

The document outlines multiple R programming tasks, including matrix operations, finding roots of quadratic equations, generating prime numbers, creating data frames, and performing statistical analyses. Each program is designed to demonstrate specific functionalities in R, such as manipulating data frames, plotting histograms, and conducting t-tests. Additionally, it includes user-defined functions and operations on factors and datasets.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

R Programming

The document outlines multiple R programming tasks, including matrix operations, finding roots of quadratic equations, generating prime numbers, creating data frames, and performing statistical analyses. Each program is designed to demonstrate specific functionalities in R, such as manipulating data frames, plotting histograms, and conducting t-tests. Additionally, it includes user-defined functions and operations on factors and datasets.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PART-A

Program No:01
Title: Write a program to create 3*3 matrix A and B and perform the following operations
Date:

A<-matrix(1:9,nrow = 3,ncol = 3,byrow = TRUE)

B<-matrix(9:1,nrow = 3,ncol = 3,byrow = TRUE)


result_a<-t(A)%*%B

result_b<-t(B)%*%(A%*%t(A))

result_c<-(A%*%t(A)%*%t(B))

result_d<-solve(B%*%(B)+(A%*%t(A))-100*diag(3))^-1

cat("AT.B\n")

print(result_a)

cat("\n BT.([Link])\n")

print(result_b)

cat("\n([Link]).BT\n")

print(result_c)

cat("\n([Link])+([Link])-100I\n")

print(result_d)
Program No:02
Title: Write R program to find roots of quadratic equation using user defined [Link] the program user supplied values
for all possible cases
Date:

quad<-function(a,b,c){
d<-b^2-4*a*c
if(d>0){
r1<-(-b+sqrt(d))/(2*a)
r2<-(-b-sqrt(d))/(2*a)
cat("\n Real roots are:",r1,"and",r2,"\n")
}
else if(d==0){
r<- -b/(2*a)
cat("\n Roots are equal:",r,"\n")
}
else{
r<- -b/(2*a)
img<-sqrt(-d)/(2*a)
c1<-complex(real = r,imaginary = img)
c2<-complex(real = r,imaginary =-img)
cat("\n Complex roots are:",c1,"and",c2,"\n")
}
}
a<-[Link](readline("Enter the value of a:"))
b<-[Link](readline("Enter the value of b:"))
c<-[Link](readline("Enter the value of c:"))

quad(a,b,c)

OUTPUT:
Program No:03
Title: Write program R script to generate prime number between two numbers
Date:
Program No:04
Title: Write an R program to create a list containing strings,numbers,vectors and logical values and do the following
manipulation over the list
Date:
Program No:05
Title: The following table shows the time takes(in minutes) by 100 students to travel to school on particular day
Time 0-5 5-10 10-15 15-20 20-25
[Link] students 5 25 40 17 13
[Link] the histogram
[Link] frequency polygon
Date:
Program No:06
Title: Write an R program to create a data Frame with following details & do the following operations
Item code Item Category Item price
1001 Electronics 700
1002 Desktop supplies 300
1003 Office Supplies 350
1004 USB 400
1005 Cp Drive 800
[Link] the data frame &display the details of only these item whose price is greater than or equal to 350
[Link] the data frame & display only items where the category is either office supplies orDesktop supplies
[Link] the data frame&display the item where the price
[Link] sum of all price
[Link] another data frame called Item-details with 3 field item code item& Reording LUI & merge the frames
Date:
data<-[Link](ItemCode=c(1001,1002,1003,1004,1005),

ItemCategory=c("Electronics","Desktop Supplies","office Supplies",

"USB","CD drive"),

ItemPrice=c(700,300,350,400,800))

print("original Dataframe")

print(data)

sa<-data[data$ItemPrice>=350,]

print("ItemPrice=350")

print(sa)

sb<-data[data$ItemCategory %in% c("Office supplies","Desktop Supplies"),]

print("Item category in 'Office Supplies'or 'Desktop Supplies' category'category")

print(sb)

sc<-data[data $ItemPrice>=300 & data $ItemPrice<=700,]

print("item price between 300 and 700:")

print(sc)

total<-sum (data $ItemPrice)

print("total sum of ItemPrice")

print(total)

item_details<-[Link](ItemCode=c(1001,1002,1003,1004,1005),
ItemQtyonHand=c(50,30,40,20,10),

ItemReorderLvl=c(10,20,15,30,5))

m_data<-merge (data,item_details,by="ItemCode")

print("merged Dataframe:")

print(m_data)

Program No:07
Title:Create a factor marital-status with levels married,single,divorced perform the following operations on this factor
[Link] the variables is a factor
[Link] the second and fourth element in the factor
[Link] third element of the factor
[Link] the second element of the factor
[Link] new level widowed to the factor and add the same level to the factor marital_status
Date:
marital_status<-factor(c("married","single","divorced","single"))

print(marital_status)

element_2nd<-marital_status[2]

element_4th<-marital_status[4]

print(paste("2nd element:",element_2nd))

print(paste("4th element:",element_4th))

marital_status <- marital_status[-3]

print(marital_status)

marital_status[2] <- "divorced"

print(marital_status)

marital_status <- factor(marital_status,levels=c(levels(marital_status),"widowed"))

print (marital_status)

Program No:08
Title:Write a R language script for following operations on Iris Datset
[Link] the Iris Dataset
[Link] first six rows of Iris dataset
Date:

library(datasets)
data("iris")

force(iris)

head(iris)

summary(iris)

cat("Number of rows:",nrow(iris),"\n")

cat("Number of coulmns:",ncol(iris),"\n")

colnames(iris)

hist(iris$[Link],main = "Histogram of sepal length",xlab="Sepal Length",

col="green",border="black")

plot(iris$[Link],iris$[Link],main="Scatterplot of [Link] vs

Sepal Length",xlab="Sepal width",ylab="Sepal Length")

boxplot(iris$[Link],iris$[Link],

main="boxplot of [Link] vs [Link]",

xlab="Sepal width",Ylab="Sepal Length")

cor(iris $ [Link],iris $ [Link],method="pearson")

cor_matrix<-cor(iris[,1:4])

cor_matrix
partB

Program:01
Title:Write R script to perfom the following using binomial distribution
i)if n=4 and p=0.10 find p(x=3)
ii)if n=12 and p=0.45,find p(5<=x<=7)
Date:
p1<-0.10

n1<-4

x1<-3
prob1<-dbinom(x1,size=n1,prob=p1)

cat("[Link] P(x=3) when n=4 and p=0.10 is",prob1,"\n")

n2<-12

p2<-0.45

x2_lower<-5

x2_upper<-7

prob2<-sum(x2_lower:x2_upper,size=n2,prob=0.45)

before_training <- c(22,20,19,24,25,25,28,22,30,27,24,18,16,19,19,28,24,25,25,23)

after_training <- c(24,22,19,22,28,26,28,24,30,29,25,20,17,18,18,28,26,27,27,24)

t_result <- [Link](before_training, after_training, paired = TRUE)


print(t_result)

if(t_result$statistic > 1.729){

cat("The training program is helpful to the student.\n")

} else {

cat("There is no significant evidence to conclude that the training program is helpful to the students.\
n")

a<-200

b<-240

prob1<-1-punif(230,min = a, max=b)

cat("[Link] p(x>230):",prob1,"\n")

prob2<-punif(220,min=a,max=b)-punif(205,min=a,max=b)

cat("[Link] p(205<=x<=220):",prob2)

You might also like