R Programming Lab Manual (1)
R Programming Lab Manual (1)
R Programming
(Laboratory)
D022822 (022)
B.TECH -VIII Semester
1. LAB INCHARGE:………………………………………
SESSION-2020-2021
List of Program as per
University SEMESTER-8th
S.
Experiments
No.
Write a program to check whether a year (integer) entered by the user is a leap
1
year or not?
Write an R program to find the sum of natural numbers without formula using the
2
if–else statement and thewhile loop.
Write a program that prints the grades of the students according to the marks
3 obtained. The grading of the marks should be as follows. Marks Grades 800-1000
A+ 700 – 800 A 500 – 700 B+ 400-500 B 150 – 400 C Less than 150 D
Write an R program to make a simple calculator that can add, subtract, multiply
4
and divide using switch cases and functions
Write a program to perform searching within a list (1 to 50). If the number is
5 found in the list, print that the search is successful otherwise print that the number
is not in the list.
Create a list and data frame that stores the marks of any three subjects for 10
6 students. Find out the total marks, average, maximum marks and minimum marks
of every subject.
Write a program to create two 3 X 3 matrices A and B and perform the following
7 operations a) Transpose of the matrix b) addition c) subtraction.
AIM: Write a program to check whether a year (integer) entered by the user is a leap year
ornot?
Description:
➢ R is the most popular data analytics tool as it is open-source, flexible, offers multiple
packages and has a huge community.
Program:
if((year %% 4) == 0)
if((year %% 100) == 0)
if((year %% 400) == 0)
else
} else {
} else {
Output:
Aim : Write an R program to find the sum of natural numbers without formula using
the if–else statement and the while loop.
Description:
Here, we ask the user for a number and display the sum of natural numbers upto that number.
We use while loop to iterate until the number becomes zero. On each iteration, we add the
number num to sum, which gives the total sum in the end.
We could have solved the above problem without using any loops using a formula.
Program:
if(num < 0) {
} else {
sum = 0
while(num > 0) {
num = num - 1
}
Output
Enter a number: 10
AIM:Write a program that prints the grades of the students according to the marks
obtained. The grading of themarks should be as follows. Marks Grades 800-1000 A+ 700 –
800 A 500 – 700 B+ 400-500 B 150 – 400 CLess than 150 D
Description:
Print grade of student by using if else decision statement in r programming. Taking the input as
marks from user and compare it and display grade according to a marks as a output.
Program:
V <-as.integer(readline(prompt="Enter a Marks:"))
if(v<1000 & v>800){
print("A+")
}else if(v<800 & v>700){
print("A")
}else if(v<700 & v>500){
print("B+")
}else if(v<500 & v>400){
print("B")
}else if(v<400 & v>150){
print("C")
}else{
print("D")
}
Output:
[1] "A+"
Program: 4
Aim: Write an R program to make a simple calculator that can add, subtract, multiply
and divide using switch cases and functions
Description:
In this program, we ask the user to choose the desired operation. Options 1, 2, 3 and 4 are
valid.
Two numbers are taken from the user and a switch branching is used to execute a particular
function.
Program:
add<- function(x, y) {
return(x + y)
subtract<- function(x, y) {
return(x - y)
multiply<- function(x, y) {
return(x * y)
divide<- function(x, y) {
return(x / y)
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
operator<- switch(choice,"+","-","*","/")
Output:
[1] "1.Add"
[1] "2.Subtract"
[1] "3.Multiply"
[1] "4.Divide"
Enter choice[1/2/3/4]: 4
AIM: Write a program to perform searching within a list (1 to 50). If the number is found
in the list, print that the search is successful otherwise print that the number is not in the
list.
Description:
List:
A list is an R-object which can contain many different types of elements inside it like vectors,
functions and even another list inside it .In this program we will perform searching within the
list.
Program:
x<-
list(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,3
4,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50)
v=7
if(v%in%x){
print("search is successful")
}else
{
print("number is not in the list")
}
Output:
AIM: Create a list and data frame that stores the marks of any three subjects for 10
students. Find out the total marks, average, maximum marks and minimum marks of every
subject.
Description:
Dataframe:
Data frames are tabular data objects. Unlike a matrix in data frame each column can contain
different modes of data. The first column can be numeric while the second column can be
character and third column can be logical. It is a list of vectors of equal length.
Data Frames are created using the data.frame() function.
Program:
s=data.frame(Name=c("Ab","Bb","Cb","Ab","A","Aaa","Aac","Abb","Acc","Add"),
S1=c(70,76,60,67,66,56,80,79,88,65),
S2=c(73,78,60,62,66,59,85,77,93,66),
S3=c(74,74,69,55,68,77,87,73,98,66))
print(s)
print(sum(s$S1))
print(max(s$S1))
print(min(s$S1))
print(mean(s$S1))
print(sum(s$S2))
print(max(s$S2))
print(min(s$S2))
print(mean(s$S2))
print(sum(s$S3))
print(max(s$S3))
print(min(s$S3))
print(mean(s$S3))
output:
Name S1 S2 S3
1 Ab 70 73 74
2 Bb 76 78 74
3 Cb 60 60 69
4 Ab 67 62 55
5 A 66 66 68
6 Aaa 56 59 77
7 Aac 80 85 87
8 Abb 79 77 73
9 Acc 88 93 98
10 Add 65 66 66
[1] 707
[1] 88
[1] 56
[1] 70.7
[1] 719
[1] 93
[1] 59
[1] 71.9
[1] 741
[1] 98
[1] 55
[1] 74.1
Program 7:
AIM: Write a program to create two 3 X 3 matrices A and B and perform the following
operations a) Transpose of the matrix b) addition c) subtraction.
Program:
print("Matrix-1:")
print(A)
print("Matrix-2:")
print(B)
r = t(A)
print(r)
r1 = t(B)
print(r1)
result = A %*% B
print(result)
result = A + B
print("Result of addition")
print(result)
result = A- B
print("Result of subtraction")
print(result)
Output:
[1,] 1 2 3
[2,] 4 5 6
[3,] 1 1 1
[1,] 0 1 2
[2,] 3 0 2
[3,] 2 2 2
[1] "Matrix-1:"
[2,] 2 5 1
[3,] 3 6 1
[1] "Matrix-2:"
[1,] 0 3 2
[2,] 1 0 2
[3,] 2 2 2
>
>print(result)
[1,] 6 5 12
[2,] 7 8 16
[3,] 8 11 20
[1,] 1 7 3
[2,] 3 5 3
[3,] 5 8 3
>
[1,] 1 1 -1
[2,] 1 5 -1
[3,] 1 4 -1
>
Program 8:
AIM:Write an R program to create a list containing strings, numbers, vectors and logical
values and do the following manipulations over the list.
a. Access the first element in the list
b. Give the names to the elements in the list
c. Add element at some position in the list
d. Remove the element
e. Print the fourth element
f. Update the third element
A List is a collection of similar or different types of data. In R, we use the list() function to
create a list. In R, each element in a list is associated with a number. The number is known as a
list index. We can access elements of a list using the index number (1, 2, 3 …).
Elements of the list can be accessed by the index of the element in the list. In case of named lists
it can also be accessed using the names. Using list(listname[index])
Update list element
replace() function in R Language is used to replace the values in the specified string vector x
with indices given in list by those given in values.
Names can be given to list elements and can be accessed using the corresponding names.
To add or append an element to the list in R use append() function. This function takes 3
parameters input list, the string or list you wanted to append, and position.
The append() function from the rlist package can also use to append one list with another in R.
Program:
print(list_data)
#(a) access 1st element in list
print(list_data[1])
print(list_data)
append(list_data,"hi",after = 2)
list_data[-2]
print(list_data[4])
replace(list_data,3,123)
Output:
>print(list_data)
[[1]]
[1] "R Program"
[[2]]
[1] "PHP"
[[3]]
[1] 4
[[4]]
[1] 5 7 9 11
[[5]]
[1] TRUE
[[6]]
[1] 125.17
[[7]]
[1] 75.83
>
>print(list_data[1])
[[1]]
>print(list_data)
$language
$`web development`
[1] "PHP"
$nubers
[1] 4
$logical
[1] 5 7 9 11
$float1
[1] TRUE
$float2
[1] 125.17
$<NA>
[1] 75.83
>
>append(list_data,"hi",after = 2)
$language
$`web development`
[1] "PHP"
[[3]]
[1] "hi"
$nubers
[1] 4
$logical
[1] 5 7 9 11
$float1
[1] TRUE
$float2
[1] 125.17
$<NA>
[1] 75.83
>
>list_data[-2]
$language
$nubers
[1] 4
$logical
[1] 5 7 9 11
$float1
[1] TRUE
$float2
[1] 125.17
$<NA>
[1] 75.83
>
> # (e)Print 4th element
>print(list_data[4])
$logical
[1] 5 7 9 11
>replace(list_data,3,123)
$language
$`web development`
[1] "PHP"
$nubers
[1] 123
$logical
[1] 5 7 9 11
$float1
[1] TRUE
$float2
[1] 125.17
$<NA>
[1] 75.83
Program 9:
AIM :Let us use the built-in dataset air quality which has Daily air quality measurements
in New York,May to September 1973. Create a histogram by using appropriate arguments
for the following statements.
e. Create a Histogram with density and Add Density curve to the histogram
Program:
data = airquality
print("Original data: Daily air quality measurements in New York, May to September 1973.")
print(class(data))
print(head(data,10))
result = data[order(data[,1]),]
print("Order the entire data frame by the first and second column:")
print(result)
output:
[1] "Original data: Daily air quality measurements in New York, May to September 1973."
[1] "data.frame"
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
10 NA 194 8.6 69 5 10
[1] "Order the entire data frame by the first and second column:"
21 1 8 9.7 59 5 21
23 4 25 9.7 61 5 23
18 6 78 18.4 57 5 18
11 7 NA 6.9 74 5 11
76 7 48 14.3 80 7 15
147 7 49 10.3 69 9 24
9 8 19 20.1 61 5 9
94 9 24 13.8 81 8 2
114 9 36 14.3 72 8 22
137 9 24 10.9 71 9 14
73 10 264 14.3 73 7 12
13 11 290 9.2 66 5 13
20 11 44 9.7 62 5 20
22 11 320 16.6 73 5 22
3 12 149 12.6 74 5 3
50 12 120 11.5 73 6 19
51 13 137 10.3 76 6 20
14 14 274 10.9 68 5 14
16 14 334 11.5 64 5 16
148 14 20 16.6 63 9 25
12 16 256 9.7 69 5 12
82 16 7 6.9 74 7 21
95 16 77 7.4 82 8 3
4 18 313 11.5 62 5 4
15 18 65 13.2 58 5 15
8 19 99 13.8 59 5 8
49 20 37 9.2 65 6 18
87 20 81 8.6 82 7 26
47 21 191 14.9 77 6 16
108 22 71 10.3 77 8 16
7 23 299 8.6 65 5 7
28 23 13 12.0 67 5 28
44 23 148 8.0 82 6 13
145 23 14 9.2 71 9 22
74 27 175 14.9 81 7 13
6 28 NA 14.9 66 5 6
38 29 127 9.7 82 6 7
19 30 322 11.5 68 5 19
24 32 92 12.0 61 5 24
64 32 236 9.2 81 7 3
129 32 92 15.5 84 9 6
17 34 307 12.0 66 5 17
78 35 274 10.3 82 7 17
97 35 NA 7.4 85 8 5
2 36 118 8.0 72 5 2
31 37 279 7.4 76 5 31
48 37 284 20.7 72 6 17
41 39 323 11.5 87 6 10
93 39 83 6.9 81 8 1
67 40 314 10.9 83 7 6
1 41 190 7.4 67 5 1
29 45 252 14.9 81 5 29
128 47 95 7.4 87 9 5
77 48 260 6.9 81 7 16
63 49 248 9.2 85 7 2
90 50 275 7.4 86 7 29
88 52 82 12.0 86 7 27
92 59 254 9.2 81 7 31
109 59 51 6.3 79 8 17
79 61 285 6.3 84 7 18
81 63 220 11.5 85 7 20
66 64 175 4.6 83 7 5
91 64 253 7.4 83 7 30
98 66 NA 4.6 87 8 6
40 71 291 13.8 90 6 9
68 77 276 5.1 88 7 7
96 78 NA 6.9 86 8 4
80 79 187 5.1 87 7 19
85 80 294 8.6 86 7 24
89 82 213 7.4 88 7 28
71 85 175 7.4 89 7 10
69 97 267 6.3 92 7 8
70 97 272 5.7 92 7 9
5 NA NA 14.3 56 5 5
10 NA 194 8.6 69 5 10
25 NA 66 16.6 57 5 25
26 NA 266 14.9 58 5 26
27 NA NA 8.0 57 5 27
32 NA 286 8.6 78 6 1
33 NA 287 9.7 74 6 2
34 NA 242 16.1 67 6 3
35 NA 186 9.2 84 6 4
36 NA 220 8.6 85 6 5
37 NA 264 14.3 79 6 6
39 NA 273 6.9 87 6 8
42 NA 259 10.9 93 6 11
43 NA 250 9.2 92 6 12
45 NA 332 13.8 80 6 14
46 NA 322 11.5 79 6 15
52 NA 150 6.3 77 6 21
53 NA 59 1.7 76 6 22
54 NA 91 4.6 76 6 23
55 NA 250 6.3 76 6 24
56 NA 135 8.0 75 6 25
57 NA 127 8.0 78 6 26
58 NA 47 10.3 73 6 27
59 NA 98 11.5 80 6 28
60 NA 31 14.9 77 6 29
61 NA 138 8.0 83 6 30
65 NA 101 10.9 84 7 4
72 NA 139 8.6 82 7 11
75 NA 291 14.9 91 7 14
83 NA 258 9.7 81 7 22
84 NA 295 11.5 82 7 23
107 NA 64 11.5 79 8 15
>hist(airquality$Ozone)
hist(temperature,
Airport",
xlim = c(50,100),
col = "darkmagenta",
freq = TRUE)
e.Create a Histogram with density and Add Density curve to the histogram
hist(temperature,
Airport",
xlim = c(50,100),
col = "darkmagenta",
freq = FALSE)
Program 10:
AIM: Design a data frame in R for storing about 8 employee details. Create a CSV file
named “input.csv” that defines all the required information about the employee such
as id, name, salary, start_date, dept. Import into R and do the following analysis.
a. Find the total number rows & columns
b. Find the maximum salary
c. Retrieve the details of the employee with maximum salary
d. Retrieve all the employees working in the IT Department
e. Retrieve the employees in the IT Department whose salary is greater than 600
and write these details into another file “output.csv”.
Description:
You can check which directory the R workspace is pointing to using the getwd() function. You
can also set a new working directory using setwd()function.
Program:
print(getwd())
setwd("/web/com")
print(getwd())
[1] "/web/com/1441086124_2016"
[1] "/web/com"
The csv file is a text file in which the values in the columns are separated by a comma. Let's
consider the following data present in the file named input.csv.
You can create this file using windows notepad by copying and pasting this data. Save the file
as input.csv using the save As All files(*.*) option in notepad.
id,name,salary,start_date,dept
1,Rick,623.3,2012-01-01,IT
2,Dan,515.2,2013-09-23,Operations
3,Michelle,611,2014-11-15,IT
4,Ryan,729,2014-05-11,HR
5,Gary,843.25,2015-03-27,Finance
6,Nina,578,2013-05-21,IT
7,Simon,632.8,2013-07-30,Operations
8,Guru,722.5,2014-06-17,Finance
Following is a simple example of read.csv() function to read a CSV file available in your current
working directory −
data<- read.csv("input.csv")
print(data)
output
By default the read.csv() function gives the output as a data frame. This can be easily checked as
follows. Also we can check the number of columns and rows.
data<- read.csv("input.csv")
print(is.data.frame(data))
print(ncol(data))
print(nrow(data))
output
[1] TRUE
[1] 5
[1] 8
Once we read data in a data frame, we can apply all the functions applicable to data frames as
explained in subsequent section.
data<- read.csv("input.csv")
sal<- max(data$salary)
print(sal)
Output
[1] 843.25
We can fetch rows meeting specific filter criteria similar to a SQL where clause.
# Create a data frame.
data<- read.csv("input.csv")
sal<- max(data$salary)
print(retval)
Output
id name salary start_datedept
data<- read.csv("input.csv")
print(retval)
Output
id name salary start_datedept
print(info)
Output
id name salary start_datedept
data<- read.csv("input.csv")
print(retval)
Output
id name salary start_datedept
R can create csv file form existing data frame. The write.csv() function is used to create the csv
file. This file gets created in the working directory.
data<- read.csv("input.csv")
write.csv(retval,"output.csv")
newdata<- read.csv("output.csv")
print(newdata)
Output
X id name salary start_datedept
Here the column X comes from the data set newper. This can be dropped using additional
parameters while writing the file.
data<- read.csv("input.csv")
newdata<- read.csv("output.csv")
print(newdata)