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

RLAB KP

Uploaded by

Akshay Hebbar
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 views

RLAB KP

Uploaded by

Akshay Hebbar
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
You are on page 1/ 16

1.

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)

K Vivek Acharya 210985


e=100*i3
solve(bbt+aat-e)

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

[,1] [,2] [,3]


[1,] 50 68 86
[2,] 122 167 212
[3,] 194 266 338

> #bt.(a.at)

[,1] [,2] [,3]


[1,] 1896 2262 2628
[2,] 2598 3099 3600
[3,] 3300 3936 4572

> #(a.at).bt

[,1] [,2] [,3]


[1,] 2412 2646 2880
[2,] 2880 3159 3438
[3,] 3348 3672 3968

> #[(b.bt)+(a.at)-100I]

K Vivek Acharya 210985


[,1] [,2] [,3]
[1,] -0.007257222 0.003189920 0.003637062
[2,] 0.003189920 -0.006441303 0.003927474
[3,] 0.003637062 0.003927474 -0.005782114

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"

3.Write R script to generate prime numbers between 2 using loops.


K Vivek Acharya 210985
Source Code:
primes_in_range<-function(lower,upper)
{
primes=c()
for(i in seq(lower,upper)){
if(i>2){
if(prime_or_not(i)==1)
primes=c(primes,i)
}
else{
primes=c(primes,i)
}}
return(primes)
}
prime_or_not<-function(num){
flag=1
for(i in 2:(num-1)){
if((num%%i)==0){
flag=0
break
}}
return(flag)
}
primes_in_range(2,30)

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.

K Vivek Acharya 210985


Source Code:
my_list <-list(
str="SDM",
num=42,
vect=c(1,2,3,4),
logi=TRUE
)

#a.access the first element in the list


first_element<-my_list[[1]]
cat("first element is :",first_element)

#b.Give name to the element in the list


names(my_list)<-c("string","number","vector","logical")
cat("\nElements after giving names\n")
print(my_list)

#c.add an element at some position in the list


new_element<-"world"
my_list<-c(my_list[1:2],new_element,my_list[3:4])
cat("\n New list after adding an element\n")
print(my_list)

#d.remove an element
my_list<-my_list[-3]
cat("\n list after removing third element:\n")
print(my_list)

#e.print the first and third element


first_element<-my_list[[1]]
third_element<-my_list[[3]]
cat("first element:",first_element,"\n")
K Vivek Acharya 210985
cat("third element:",third_element,"\n")

#f.update the third element


my_list[[3]]<-c(5,6,7)
#print the updated list
print(my_list)

OUTPUT:

first element is : SDM>


> #b.Give name to the element in the list

Elements after giving names


$string
[1] "SDM"

$number
[1] 42

$vector
[1] 1 2 3 4

$logical
[1] TRUE

>
> #c.add an element at some position in the list

New list after adding an element


$string
[1] "SDM"

$number
[1] 42

[[3]]
[1] "world"

$vector
[1] 1 2 3 4

$logical
[1] TRUE

K Vivek Acharya 210985


>
> #d.remove an element

list after removing third element:


$string
[1] "SDM"

$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.

K Vivek Acharya 210985


Time 0-5 5-10 10-15 15-20 20-25
No .of 5 25 40 17 13
Students

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)

#b.Subset & display items in specified categories.


subset_b<-data[data$ItemCategory %in%c("office Supplies","Desktop Supplies"),]
print("subset of items in 'office supplies' or'desktop supplies' category:")
print(subset_b)

#c.subset & display items with price b/w 300&700


subset_c<-data[data$ItemPrice>=300&data$ItemPrice<=700,]
print("subset of items with price b/w 300&700:")
print(subset_c)

K Vivek Acharya 210985


#d.compute the sum of a Itemprice
total_price<-sum(data$ItemPrice)
print(paste("Total sum of itemprice:",total_price))
#e.create another data frame 'item_details'
Item_details<-data.frame(
ItemCode=c(1001,1002,1003,1004,1005),
ItemQtyonHand=c(10,20,15,5,12),
ItemReorderLvl=c(5,10,8,2,7)
)

#merge the 2 dataframes on'ItemCode'


merged_data<-merge(data,Item_details,by="ItemCode")
print("Merged Data Frame:")
print(merged_data)

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
>

> #b.Subset & display items in specified categories.


[1] "subset of items in 'office Supplies' or'Desktop Supplies' category:"
ItemCode ItemCategory ItemPrice
2 1002 Desktop Supplies 300
3 1003 Office Supplies 350
>

> #c.subset & display items with price b/w 300&700

K Vivek Acharya 210985


ItemCode ItemCategory ItemPrice
1 1001 Electronics 700
2 1002 Desktop Supplies 300
3 1003 Office Supplies 350
4 1004 USB 400
>

> #d.compute the sum of a Itemprice


[1] "Total sum of itemprice: 2550"

> #e.create another data frame 'item_details'


+ ItemCode=c(1001,1002,1003,1004,1005),
+ ItemQtyonHand=c(10,20,15,5,12),
+ ItemReorderLvl=c(5,10,8,2,7)

> #merge the 2 dataframes on'ItemCode'


ItemCode ItemCategory ItemPrice ItemQtyonHand ItemReorderLvl
1 1001 Electronics 700 10 5
2 1002 Desktop Supplies 300 20 10
3 1003 Office Supplies 350 15 8
4 1004 USB 400 5 2
5 1005 CD Drive 800 12 7
>

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.

K Vivek Acharya 210985


b) Access second and forth element and print them.
c) Remove the third element from the factor and print it.
d) Modify the second element of the factor and print it.
e) Add new level "widowed" to the factor and the same level to the factor
martial_status.
Source Code:
#create the factor variable with explicit levels
martial_status<-
factor(c("married","single","divorced","married","single"),levels=c("married","single","divor
ced"))

#a.check if the variable is a factor and print it.


is_factor<-is.factor(martial_status)
cat("is martial_status a factor?",is_factor,"\n")
cat("Martial_status:",as.character(martial_status),"\n")

#b.access second and forth element and print them.


second_element<-martial_status[2]
fourth_element<-martial_status[4]
cat("second element:", as.character(second_element),"\n")
cat("fourth element:", as.character(fourth_element),"\n")

#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")

#d.Modify the second element of the factor and print it.


martial_status[2]<-"married"
cat("martial-status after modifying second element:",
as.character(martial_status),"\n")

#e.add new level "widowed" to the factor and print it.


K Vivek Acharya 210985
martial_status<-factor(martial_status,levels=c(levels(martial_status),"widowed"))
cat("Martial-status after adding 'widowed' level:",
as.character(levels(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

#b.access second and forth element and print them.


second element: single

fourth element: married

#c.Remove the third element from the factor and print it.
Martial-status after removing the third element: married single married single

#d.Modify the second element of the factor and print it


martial-status after modifying second element: married married married single

#e.add new level "widowed" to the factor and print it.


Martial-status after adding 'widowed' level: married single divorced widowed

K Vivek Acharya 210985

You might also like