R Lab Manual
R Lab Manual
PART A:
1.Write a R program for different types of data structures in R
print("Vectors")
X = c(1, 3, 5, 7, 8)
print(X)
print("List")
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)
print("DataFrames")
Name = c("Amiya", "Raj", "Asish")
Language = c("R", "Python", "Java")
Age = c(22, 25, 45)
df = data.frame(Name, Language, Age)
print(df)
print("Matrix")
A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3, ncol = 3,
byrow = TRUE)
print(A)
print("Array")
A = array(c(1, 2, 3, 4, 5, 6, 7, 8),
dim = c(2, 2, 2) )
print(A)
print("factors")
fac = factor(c("Male", "Female", "Male",
"Male", "Female", "Male", "Female"))
print(fac)
Output:
"Vectors"
[1] 1 3 5 7 8
"List"
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]]
[1] 4
"DataFrames"
"Matrix"
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
"Array"
,,1
[,1] [,2]
[1,] 1 3
[2,] 2 4
,,2
[,1] [,2]
[1,] 5 7
[2,] 6 8
"factors"
[1] Male Female Male Male Female Male Female
Levels: Female Male
a = TRUE
print(a)
print(class(a))
A = 14L
print(A)
print(class(A))
x = 13.4
print(x)
print(class(x))
alphabet = "a"
print(alphabet)
print(class(alphabet))
Output:
[1] TRUE
[1] "logical"
[1] 14
[1] "integer"
[1] 13.4
[1] "numeric"
[1] "a"
[1] "character"
[1] "Welcome to Programiz!"
[1] "character"
3.Write a R program that include different operators, control structures, default values for
arguments, returning complex objects
a. operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)
Addition of vectors : 2 5
Subtraction of vectors : -2 -1
Multiplication of vectors : 0 6
Division of vectors : 0 0.6666667
Modulo of vectors : 0 2
Power operator : 0 8
b. control structures
x <-5
Output:
"5 is less than 10"
c.default values for arguments
divisible <- function(a, b = 3){
if(a %% b == 0)
{
return(paste(a, "is divisible by", b))
}
else
{
return(paste(a, "is not divisible by", b))
}
}
print(divisible(10, 5))
print(divisible(12))
Output:
[1] "10 is divisible by 5"
[1] "12 is divisible by 3"
Output:
[1] "red"
[1] 20
[1] "round"
left<-arr[which(arr<= pivot)]
right<-arr[which(arr> pivot)]
if (length(left) > 1)
{
left <- quickSort(left)
}
if (length(right) > 1)
{
right <- quickSort(right)
}
Output:
"before sorting"
[1] 48 49 8 26 67 19 38 99 92 34
[1] "after sorting"
[1] 8 19 26 34 38 48 49 67 92 99
val = NULL,
left = NULL,
right = NULL,
}
)
)
classname = 'BST',
public = list(
root = NULL,
# node = NULL,
node=NULL,
insert = function(data){
if(is.null(self$root)){
self$node <- Node$new(data)
}else{
self$insert_recur(data, self$root)
}
},
},
get_height = function(cur_node){
if(is.null(cur_node$val)){
return(-1)
}else{
return(max(self$get_height(cur_node$left),self$get_height(cur_node$right))+1)
}
}
print(bst$get_height(bst$root))
print(bst$get_height(bst$root$right))
print(bst$root)
print(bst$root$left)
print(bst$root$right)
print(bst$root$left$left)
print(bst$root$left$right)
Output:
<BST>
Public:
clone: function (deep = FALSE)
get_height: function (cur_node)
insert: function (data)
insert_recur: function (data, cur_node)
node: NULL
root: NULL
<Node>
Public:
clone: function (deep = FALSE)
initialize: function (val = NULL, left = NULL, right = NULL)
left: NULL
right: NULL
val: 3
<Node>
Public:
clone: function (deep = FALSE)
initialize: function (val = NULL, left = NULL, right = NULL)
left: NULL
right: NULL
val: 2
<Node>
Public:
clone: function (deep = FALSE)
initialize: function (val = NULL, left = NULL, right = NULL)
left: NULL
right: NULL
val: 3
<Node>
Public:
clone: function (deep = FALSE)
initialize: function (val = NULL, left = NULL, right = NULL)
left: NULL
right: NULL
val: 5
<Node>
Public:
clone: function (deep = FALSE)
initialize: function (val = NULL, left = NULL, right = NULL)
left: NULL
right: NULL
val: 6
[1] -1
[1] -1
NULL
NULL
NULL
5.Write a R program for calculating cumulative sums, and products minima maxima and
calculus.
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
print(cumsum(x1))
print(cumsum(x2))
x1 <- c(2, 4, 5, 7)
x2 <- c(2.4, 5.6, 3.4)
print(cumprod(x1))
print(cumprod(x2))
x1 <- c(3, 6, 4, 2, 6, 7)
x2 <- c(-4, 6, 3)
print(cummin(x1))
print(cummin(x2))
x1 <- c(3, 6, 4, 2, 6, 7)
x2 <- c(-4, 6, 3)
print(cummax(x1))
print(cummax(x2))
Output:
Output:
[1] 1 1 1
7.Write a R program that include linear algebra operations on vectors and matrices
a =matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow =3,
ncol =3,
byrow =TRUE
)
cat("The 3x3matrix:\n")
print(a)
Output:
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Adding 1 to every element:
[,1] [,2] [,3]
[1,] 2 3 4
[2,] 5 6 7
[3,] 8 9 10
Subtracting 3 from each element:
[,1] [,2] [,3]
[1,] -2 -1 0
[2,] 1 2 3
[3,] 4 5 6
Multiplying each element by 10:
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 40 50 60
[3,] 70 80 90
Squaring each element:
[,1] [,2] [,3]
[1,] 1 4 9
[2,] 16 25 36
[3,] 49 64 81
Doubled each element of original matrix:
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 8 10 12
[3,] 14 16 18
8.Write a R program for any visual representation of an object with creating graphs using
graphic functions: Plot(),Hist(),Linechart(),Pie(),Boxplot(),Scatterplots().
A. Plot:
barplot(airquality$Ozone,
main = 'Ozone Concenteration in air',
xlab = 'ozone levels', horiz = TRUE)
Output:
B.Histogram:
data(airquality)
Output:
C.Linechart:
v <- c(17, 25, 38, 23, 41)
plot(v, type = "o")
Output:
D. piechart:
geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
pie(geeks, labels)
Output:
E.BoxPlot:
data(mtcars)
boxplot(disp ~ gear, data = mtcars,
main = "Displacement by Gear",
xlab = "Gear",
ylab = "Displacement")
Output:
F.scatterplot:
input <- mtcars[, c('wt', 'mpg')]
Output:
9.Write a R program for with any dataset containing data frame objects, indexing and
sub setting data frames, and employ manipulating and analysing data.
stats <- data.frame(employe=c('A', 'B', 'C', 'D'),
salary=c(100, 200, 408, 23),
bonuspoint=c(17, 20, 3, 5))
print("stats Dataframe")
print(stats)
print(subset(x=stats, subset=bonuspoint>5, select=c(employe,bonuspoint)))
Output:
"stats Dataframe"
employebonuspoint
1 A 17
2 B 20
print(model)
png(file = "linearRegGFG.png")
dev.off()
Output:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
-39.7137 0.6847
PART B:
1.write a R program to find the factorial of a number
fact<-function(num){
answer=1
for(i in 1:num){
answer=answer*i
}
print("factorial of a number is")
print(answer)
}
value <- readline(prompt="Enter value: ")
fact(value)
Output:
Enter value: 3
[1] "factorial of a number is"
[1] 6
}
Output:
Enter a number: 10
[1] "10 is not an Armstrong number"
add <- fv + sv
cat("The final vector is: ", add)
Output:
The first vector is: 1 2 3 4
The second vector is: 5 6 7 8
The final vector is: 6 8 10 12
return(recurse_fibonacci(n-1) + recurse_fibonacci(n-2))
}
}
if(nterms <= 0) {
print("please enter a positive integer")
} else {
print("Fibonacci Sequence: ")
for(i in 0:(nterms - 1)){
print(recurse_fibonacci(i))
}
}
Output:
How many terms? 5
[1] "Fibonacci Sequence: "
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
Output:
Enter first number: 72
Enter second number: 124
[1] "The H.C.F. of 72 and 124 is 4"
Output:
[1] "sorted array is"
[1] 1 2 3 4 5 6 7 8 9 10 11
[1] "3rd mininum element"
[1] 3
[1] "3rd maximum element"
[1] 9
if(flag == 0) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}
Output:
Enter a number: 3
[1] "3 is a prime number"
Enter a number: 9
[1] "9 is not a prime number"