R Language Program
Q1. Write an R program to find the
maximum and the minimum value of a given vector.
Ans:
> nums = c(10, 20, 30, 40, 50, 60)
>
print('Original vector:')
[1]
"Original vector:"
>
print(nums)
[1] 10 20 30
40 50 60
>
print(paste("Maximum value of the said vector:",max(nums)))
[1] "Maximum
value of the said vector: 60"
>
print(paste("Minimum value of the said vector:",min(nums)))
[1]
"Minimum value of the said vector: 10"
Q.2 Write an R program to sort a Vector in ascending and descending order.
Ans:
> x = c(10,
20, 30, 25, 9, 26)
>
print("Original Vectors:")
[1]
"Original Vectors:"
>
print(x)
[1] 10 20 30
25 9 26
>
print("Sort in ascending order:")
[1]
"Sort in ascending order:"
>
print(sort(x))
[1] 9 10 20 25 26 30
>
>
print("Sort in descending order:")
[1]
"Sort in descending order:"
>
print(sort(x, decreasing=TRUE))
[1] 30 26 25
20 10 9
Q3. Write an R program to compare
two data frames to find the elements in first data frame that are not present in second data frame.
Ans:-
>df_92 = data.frame(
+ "item" = c("item1",
"item2", "item3"),
+ "Jan_sale" = c(12, 14, 12),
+ "Feb_sale" = c(11, 12, 15),
+ "Mar_sale" = c(12, 14, 15)
+ )
> df_93 = data.frame(
+ "item" = c("item1",
"item2", "item3"),
+ "Jan_sale" = c(12, 14, 12),
+ "Feb_sale" = c(11, 12, 15),
+ "Mar_sale" = c(12, 15, 18)
+
+ )
> print("Original
Dataframes:")
[1] "Original
Dataframes:"
> print(df_92)
item Jan_sale Feb_sale Mar_sale
1 item1 12
11 12
2 item2 14
12 14
3 item3 12
15 15
> print(df_93)
item Jan_sale Feb_sale Mar_sale
1 item1 12
11 12
2 item2 14
12 15
3 item3 12
15 18
> print("Row(s) in first
data frame that are not present in second data frame:")
[1] "Row(s) in first data
frame that are not present in second data frame:"
> print(setdiff(df_92,df_93))
Mar_sale
1 12
2 14
3 15
>
Ans:
>
print("First 10 letters in lower case:")
[1]
"First 10 letters in lower case:"
> t =
head(letters, 10)
>
print(t)
[1] "a" "b" "c"
"d" "e" "f" "g" "h"
"i" "j"
>
print("Last 10 letters in upper case:")
[1]
"Last 10 letters in upper case:"
> t =
tail(LETTERS, 10)
>
print(t)
[1] "Q" "R" "S"
"T" "U" "V" "W" "X"
"Y" "Z"
>
>
print("Letters between 22nd to 24th letters in upper case:")
[1]
"Letters between 22nd to 24th letters in upper case:"
> e =
tail(LETTERS[22:24])
>
print(e)
[1]
"V" "W" "X"
>
Q.5 Write an R program to find Sum, Mean and Product of a Vector.
Ans:-
> x = c(10, 20, 30)
>
print("Sum:")
[1]
"Sum:"
>
print(sum(x))
[1] 60
>
print("Mean:")
[1]
"Mean:"
>
print(mean(x))
[1] 20
>
print("Product:")
[1]
"Product:"
>
print(prod(x))
[1] 6000
Ans:
> marks =
c(70, 95, 80, 74)
>
barplot(marks,
+ main = "Comparing marks of 5
subjects",
+ xlab = "Marks",
+ ylab = "Subject",
+ names.arg = c("C++",
"Javascript", "PHP", "Rscript."),
+ col = "darkred",
+ horiz = FALSE)
>
Q.7 Write an R program to create a Dataframes which contain details of 5 employees and display the details in ascending order
Ans:-
>
>
Employees = data.frame(Name=c("Kiran K","Ravindra
K","Sudhir A", "Mahesh N","Rupali"),
+
Gender=c("M","M","M","M","F"),
+ Age=c(23,22,25,26,32),
+
+
+ Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"),
+
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576")
+ )
>
print("Details of the employees:")
[1]
"Details of the employees:"
>
print(Employees)
Name Gender Age
Designation SSN
1 Kiran K
M 23 Clerk 123-34-2346
2 Ravindra
K M
22 Manager 123-44-779
3 Sudhir A
M 25 Exective 556-24-433
4 Mahesh N
M 26 CEO
123-98-987
5 Rupali
F 32 ASSISTANT 679-77-576
>
Q.8 Write an R program to create a data frame using two given vectors and display the duplicated elements and unique rows of the said data frame.
Ans:-
> a =
c(10,20,10,10,40,50,20,30)
> b = c(10,30,10,20,0,50,30,30)
> print("Original data frame:")
[1] "Original data frame:"
> ab = data.frame(a,b)
> print(ab)
a b
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30
> print("Duplicate elements of the said data
frame:")
[1] "Duplicate elements of the said data frame:"
> print(duplicated(ab))
[1] FALSE FALSE TRUE
FALSE FALSE FALSE TRUE FALSE
> print("Unique rows of the said data frame:")
[1] "Unique rows of the said data frame:"
> print(unique(ab))
a b
1 10 10
2 20 3
4 10 20
5 40 0
6 50 50
8 30 30
>
Q.9 Write an R program to change the first level of a factor with another level of a given factor.
Ans:-
> v = c("a", "b", "a",
"c", "b")
> print("Original vector:")
[1] "Original vector:"
> print(v)
[1] "a" "b" "a" "c"
"b"
> f = factor(v)
> print("Factor of the said vector:")
[1] "Factor of the said vector:"
> print(f)
[1] a b a c b
Levels: a b c
> levels(f)[1] = "e"
> print(f)
[1] e b e c b
Levels: e b c
>
>
1) Give names to the elements in the list.
2) Add an element at the end of the list.
3) Remove the last element.
4) Update the 3rd Element
Ans:-
> my_list
<-list("Pune","Mumbai","Nashik")
> my_list
[[1]]
[1] "Pune"
[[2]]
[1] "Mumbai"
[[3]]
[1] "Nashik"
> append(my_list,"Delhi")
[[1]]
[1] "Pune"
[[2]]
[1] "Mumbai"
[[3]]
[1] "Nashik"
[[4]]
[1] "Delhi"
> print("Remove the last element of the list:")
[1] "Remove the last element of the list:"
> my_list[-4]
[[1]]
[1] "Pune"
[[2]]
[1] "Mumbai"
[[3]]
[1] "Nashik"
> print("Update the third element of the
list:")
[1] "Update the third element of the list:"
> my_list[3] =
"Shirdi"
> print("New list:")
[1] "New list:"
> print(my_list)
[[1]]
[1] "Pune"
[[2]]
[1] "Mumbai"
[[3]]
[1] "Shirdi
Q.11 Write
a script in R to create two vectors of different lengths and give these vectors
as input to array and print addition and subtraction of those matrices.
Ans:- > matrix1 <- matrix(c(3, 9,
-1, 4, 2, 6), nrow = 2)
>
print(matrix1)
[,1] [,2] [,3]
[1,] 3 -1 2
[2,] 9
4 6
> matrix2
<- matrix(c(5, 2, 0, 9, 3, 4), nrow = 2)
>
print(matrix2)
[,1] [,2] [,3]
[1,] 5
0 3
[2,] 2
9 4
> result
<- matrix1 + matrix2
>
cat("Result of addition","\n")
Result of
addition
>
print(result)
[,1] [,2] [,3]
[1,] 8
-1 5
[2,] 11
13 10
> result
<- matrix1 - matrix2
>
cat("Result of subtraction","\n")
Result of
subtraction
>
print(result)
[,1] [,2] [,3]
[1,] -2
-1 -1 [2,]
7 -5
Q.12
Write an R Program to calculate Multiplication Table
Ans:-
> num =
as.integer(readline(prompt = "Enter a number: "))
Enter a
number: 4
> for(i
in 1:10)
+ {
print(paste(num,'x', i, '=', num*i)) }
[1] "4
x 1 = 4"
[1] "4
x 2 = 8"
[1] "4
x 3 = 12"
[1] "4
x 4 = 16"
[1] "4
x 5 = 20"
[1] "4
x 6 = 24"
[1] "4
x 7 = 28"
[1] "4
x 8 = 32"
[1] "4
x 9 = 36"
[1] "4
x 10 = 40"
>
i) Create a variable “y” and attach to it the
output attribute of the “iris” dataset.
ii) Create a barplot to breakdown your output
attribute.
iii)
Create a density plot matrix for each attribute by class value
Ans:-
>
data("iris")
> dataset
<- iris
> dataset
<- iris
>
colnames(dataset) <- c("Sepal.Length", "Sepal.Width",
"Petal.Lenght", "Petal.Width", "Species")
>
library(caret)
>
library(mlbench)
>
validation_index <- createDataPartition(dataset$Species, p=0.80, list=FALSE)
>
validation <- dataset[-validation_index,]
> dataset
<- dataset[validation_index,]
>
dim(dataset)
[1] 120 5
>
sapply(dataset, class)
Sepal.Length Sepal.Width Petal.Lenght Petal.Width Species
"numeric" "numeric" "numeric" "numeric" "factor"
>
head(dataset)
Sepal.Length Sepal.Width Petal.Lenght
Petal.Width Species
1 5.1 3.5 1.4 0.2
setosa
2 4.9 3.0 1.4 0.2
setosa
3 4.7 3.2 1.3 0.2
setosa
4 4.6 3.1 1.5 0.2
setosa
5 5.0 3.6 1.4 0.2
setosa
6 5.4
3.9 1.7 0.4
setosa
>
levels(dataset$Species
[1]
"setosa"
"versicolor" "virginica"
>
percentage <- prop.table(table(dataset$Species)) * 100
>
cbind(freq=table(dataset$Species), percentage=percentage)
freq percentage
setosa 40
33.33333
versicolor 40
33.33333
virginica 40
33.33333
>
summary(dataset)
Sepal.Length Sepal.Width Petal.Lenght Petal.Width Species
Min.
:4.300 Min. :2.200
Min. :1.000 Min.
:0.100 setosa :40
1st Qu.:5.100
1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:40
Median :5.800
Median :3.000 Median :4.200 Median :1.300 virginica :40
Mean
:5.858 Mean :3.049
Mean :3.773 Mean
:1.202
3rd Qu.:6.425
3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max.
:7.900 Max. :4.400
Max. :6.900 Max.
:2.500
> x <-
dataset[,1:4]
> y <-
dataset[,5]
> plot(y)
> scales
<- list(x=list(relation="free"), y=list(relation
="free"))
>
featurePlot(x=x,y=y, plot = "density", scales = scales)
>
>
Q.14.Write an R program to concatenate two given factor in a single factor and display in descending order.
Ans:-
> f1 <- factor(sample(LETTERS, size=6,
replace=TRUE))
> f2 <- factor(sample(LETTERS, size=6,
replace=TRUE))
> print("Original factors:")
[1] "Original factors:"
> print(f1)
[1] A Y W N Q U
Levels: A N Q U W Y
> print(f2)
[1] C J Z L V C
Levels: C J L V Z
> f = factor(c(levels(f1)[f1], levels(f2)[f2]))
> print("After concatenate factor becomes:")
[1] "After concatenate factor becomes:"
> print(f)
[1] A Y W N Q U C J
Z L V C
Levels: A C J L N Q U V W Y Z
>
> print("sort in descending order:")
[1] "sort in descending order:"
> print(sort(f,decreasing = FALSE))
[1] A C J L N Q U V W Y Z
Levels: A C J L N Q U V W Y Z
Q.15.Write an R program to extract the
five of the levels of factor created from a random sample from the LETTERS
Ans:-
> L = sample(LETTERS,size=50,replace=TRUE)
> print("Original data:")
[1] "Original data:"
> print(L)
[1] "Z"
"D" "I" "P" "K" "Q"
"L" "N" "U" "W" "H"
"Q" "M" "E" "C" "B"
"D" "V" "V" "V"
[21] "L" "I" "N"
"Q" "H" "D" "S" "E"
"H" "P" "R" "S" "G"
"Y" "Q" "I" "M" "W"
"M" "X"
[41] "B" "S" "J"
"X" "K" "D" "C" "F"
"W" "I"
> f = factor(L)
> print("Original factors:")
[1] "Original factors:"
> print(f)
[1] Z D I P K Q L N
U W H Q M E C B D V V V L I N Q H D S E H P R S G Y Q I M W M X
[41] B S J X K D C F W I
Levels: B C D E F G H I J K L M N P Q R S U V W X Y Z
> print("Only five of the levels")
[1] "Only five of the levels"
> print(table(L[1:5]))
D I K P Z
1 1 1 1 1
0 Comments
Please do not enter any spam link in the comment box.