RLAB KP
RLAB KP
Write a R program to create a 3x3 matrices A and B and perform the following
operation.
Source Code:
a=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
b=matrix(c(7,8,9,10,11,12,13,14,15),nrow=3,ncol=3)
a
b
num_of_rows=nrow(a)
num_of_cols=ncol(b)
prod=matrix(,nrow=num_of_rows,ncol=num_of_cols)
at<-t(a)
at
#at.b
prod=at%*%b
prod
#bt.(a.at)
bt<-t(b)
temp=matrix(,nrow=num_of_rows,ncol=num_of_cols)
temp=a%*% at
temp
prod=bt%*%temp
prod
#(a.at).bt
prod=temp%*%bt
prod
#[(b.bt)+(a.at)-100I]
bbt<-b%*%bt
aat<-a%*%at
i3<-diag(nrow=3)
OUTPUT:
>a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
>b
[,1] [,2] [,3]
[1,] 7 10 13
[2,] 8 11 14
[3,] 9 12 15
> #at
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> #at.b
> #bt.(a.at)
> #(a.at).bt
> #[(b.bt)+(a.at)-100I]
2.Write a R program to find roots of quadratic equation using user defined function.
K Vivek Acharya 210985
Test the program user supplied values for all possible values.
Source Code:
quadraticRoots<-function(a,b,c){
print(paste("you have choosen the quadratic equation",a,"x^2+",b,"x+",c))
discriminant<-(b^2)-(4*a*c)
if(discriminant<0){
return(paste("this quadratic equation has no real numbered roots"))
}
else if(discriminant>0){
x_int_plus<-(-b+sqrt(discriminant))/(2*a)
x_int_neg<-(-b-sqrt(discriminant))/(2*a)
return(paste("the two x-intercepts for the quadratic equation are",
format(round(x_int_plus,5),nsmall=5),"and",
format(round(x_int_neg,5),nsmall=5),"."
))
}
else{
x_int<-(-b)/(2*a)
return(paste("the quadratic equation has only one root.the root is",x_int))
}
}
quadraticRoots(6,8,2)
quadraticRoots(2,4,2)
quadraticRoots(2,2,2)
OUTPUT:
K Vivek Acharya 210985
> quadraticRoots(6,8,2)
[1] "you have choosen the quadratic equation 6 x^2+ 8 x+ 2"
[1] "the two x-intercepts for the quadratic equation are -0.33333 and -1.00000 ."
> quadraticRoots(2,4,2)
[1] "you have choosen the quadratic equation 2 x^2+ 4 x+ 2"
[1] "the quadratic equation has only one root.the root is -1"
> quadraticRoots(2,2,2)
[1] "you have choosen the quadratic equation 2 x^2+ 2 x+ 2"
[1] "this quadratic equation has no real numbered roots"
OUTPUT:
> primes_in_range(2,30)
[1] 2 3 5 7 11 13 17 19 23 29
4.Write a R Program to create a list with Strings, Numbers, Vectors & Logical values.
#d.remove an element
my_list<-my_list[-3]
cat("\n list after removing third element:\n")
print(my_list)
OUTPUT:
$number
[1] 42
$vector
[1] 1 2 3 4
$logical
[1] TRUE
>
> #c.add an element at some position in the list
$number
[1] 42
[[3]]
[1] "world"
$vector
[1] 1 2 3 4
$logical
[1] TRUE
$number
[1] 42
$vector
[1] 1 2 3 4
$logical
[1] TRUE
>
> #e.print the first and third element
first element: SDM
third element: 1 2 3 4
>
> #f.update the third element
)
$string
[1] "SDM"
$number
[1] 42
$vector
[1] 5 6 7
$logical
[1] TRUE
5.The Following Table Shows the time taken by 100 Students to Travel to school on a
Particular Day.
Source Code:
library(ggplot2)
#create a data frame from the provided data
data<-data.frame(
time=c("0-5","5-10","10-15","15-20","20-25"),
NoOfStudents=c(5,25,30,17,13)
)
#calculate the mid points of the time interval
data$Midpoint<-c(2.5,7.5,12.5,17.5,22.5)
#create a histogram
hist_plot<-ggplot(data,aes(x=Midpoint,y=NoOfStudents))+
geom_bar(stat="identity",width=5,fill="red",color="black")+
labs(title="Travel Time Histogram",
x="Time(minutes)",
y="Number of Students")
#create frequency polygon
freq_polygon_plot<-ggplot(data,aes(x=Midpoint,y=NoOfStudents))+
geom_line(color="blue",size=1)+
geom_point(color="blue",size=3)+
labs(title="Frequency Polygon",
x="Time(minutes)",
y="Number of Students")
#display both plots side by side
library(gridExtra)
grid.arrange(hist_plot,freq_polygon_plot,ncol=2)
OUTPUT:
K Vivek Acharya 210985
6. Write an R program to create to create a Data Frame with following details and do
the following operations.
Item Code Item Category Item Price
1001 Electronics 700
K Vivek Acharya 210985
1002 Desktop Supplies 30
1003 Office Supplies 350
1004 USB 400
1005 CD Drive 800
a. Subset the Data Frame &display the details of only those items whose price is greater
than or equal to 350.
b. Subset the Data Frame &display the details of only the items where the category is
either “Office Supplies” or “Desktop Supplies”.
c. Subset the Data Frame &display the items where the Item Price between 300 & 700.
d. Compute the sum of all Item Price.
e. Create another Data Frame called “item-details” with three different fields
ItemCode, ItemQtyonHand and ItemReorderLvl and merge the 2 frames.
Source Code:
data<-data.frame(
ItemCode=c(1001,1002,1003,1004,1005),
ItemCategory=c("Electronics","Desktop Supplies","office Supplies","USB","CD Drive"),
ItemPrice=c(700,300,350,400,800)
)
#a.subset & display items with price>=350
subset_a<-data[data$ItemPrice>=350,]
print("subset of items with price>=350:")
print(subset_a)
OUTPUT:
> #a.subset & display items with price>=350
ItemPrice ItemCategory ItemPrice
1 1001 Electronics 700
3 1003 Office Supplies 350
4 1004 USB 400
5 1005 CD Drive 800
>
Create a factor Martial status with levels Married ,Single, Divorced ,Perform the
following operations on their factors.
a) Check if the variable is a factor and print it.
#c.Remove the third element from the factor and print it.
martial_status<-martial_status[-3]
cat("Martial-status after removing the third element:",
as.character(martial_status),"\n")
OUTPUT:
#a.check if the variable is a factor and print it.
is martial_status a factor? TRUE
Martial_status: married single divorced married single
#c.Remove the third element from the factor and print it.
Martial-status after removing the third element: married single married single