You are on page 1of 25

R programming 1

A
Practical File
On
Software Lab
BCA-607- S/W Lab – R, Programming
(Based on BCA-602)

BCA-6th Semester
(Session: 2019-22)

SubmittedTo: SubmittedBy:
Mrs.SavitaWadhwan Sharvi
1319036

M.M. Institute of Computer Technology & Business


Management,

Maharishi Markandeshwar Deemed To Be University,


Mullana (Ambala)
R programming 2

INDEX

Sr. No. Practical Name Page Signature


No.
1. Write a R Program to perform following operations on 4-5
Vector:
Min(), max(), mean(), sqrt(), length(), sum(), prod(),
sort()(in ascending and descending order), rev(),
addition/subtraction/multiplication/division of two
vectors.
2. Write a R Program to create a list containing a vector, a 6-9
matrix and a list and perform following operations:
i. Update the elements in thelist.
ii. Merge two lists into onelist.
iii. Count number of objects in a givenlist.
3. Write a program to check leap year in R. 10

4. Write a Program to print the numbers from 1 to 15 in 11-12


such a way that it prints “PUFF” for multiples of 3, it
prints “CHUG” for multiples of 5, and print
“PUFFCHUG” for multipes of both else it prints the
number.
5. Write a program to search a number in a list. If the 13-14
number is found then print its position else print -1.
6. Create two lists to store information of two Students like 15-16
Roll_No, Name, Age, Subjects. Perform operations to
Print two lists, add new component Gender to two
students, Access RollNo. And Subjects of Student1,
Access RollNo. And subjects of Student2. Merge
Subjects into new List Subjects_All.
7. Write a User defined function to generate Fibonacci 17-18
series using while loop.
8. Write a Program in R to make a simple calculator. 19-20
9. Write a R Program to create an 3 dimensional array of 21-22
30 elements using the dim()function.
10. Write a R Program to create two Matrix and add, 23-24
subtract, Multiply, and divide the matrices.
11. Write a R program to create a matrix and perform
following operations:
R programming 3

i. Extract the sub matrix whose rows have


column value> 3 from a givenmatrix.
ii. Convert a matrix to a 1 dimensionalarray.
iii. Convert a given matrix to a list ofcolumn-Vectors.
12. Write a R program to create a data frame and get the
structure, statistical summary and nature of data of a
given data frame.
13. Write a R program to find sum of even and odd numbers
between 1 and 20.
14. Write a program to check whether a number is
Armstrong number or not.
15. Write a Program to import a .csv file and create a
dataframe and export to csv file. Use read.csv() and
write.csv() functions.
16. Write a R program to draw various graphs in R.
R programming 4

Practical-1

AIM:- Write a R Program to perform following operations on Vector:


Min(), max(), mean(), sqrt(), length(), sum(),
prod(), sort()(in ascending and descending
order),rev(),addition/subtraction/multiplication/
division of two vectors.
Source Code:-
v1 <- c(5,2,1,7,6)
v2 <- c(14,10,8,6,9)
print("Minimum element in vector v1is")
print(min(v1))
print("Minimum element in vector v2is")
print(min(v2))
print("Maximum element in vector v1is")
print(max(v1))
print("Maximum element in vector v2is")
print(max(v2))
print("Mean of vector v1")
print(mean(v1))
print("Mean of vector v2")
print(mean(v2))
print("Sum of vector v1")
print(sum(v1))
print("Sum of vector v2")
print(sum(v2))
print("Product of vector v1")
print(prod(v1))
print("Product of vector v2")
print(prod(v2))
print("sorting of v1")
print(sort(v1))
print("sorting of v2 in decreasing order")
print(sort(v2,decreasing=T))
print("Oder on V2")
print(order(v2))
print("Oder on V2 indescending")
print(order(v2,decreasing =T))
print("Reverse of vectorr v1")
print(rev(v1))
print("Reverse of vectorr v2")
print(rev(v2))
print("Sum of v1 and v2 is")
R programming 5

print((v1+v2))
print("Subtraction of v1 and v2 is")
print((v1-v2))
print("Production of v1 and v2 is")
print((v1*v2))
print("Division of v1 and v2 is")
print((v1/v2))
print("Length of vector v1")
print(length(v1))
print("Length of vector v2")
print(length(v2))

OUTPUT-
R programming 6

Practical-2

AIM:- Write a R programming create a list containing vector, a matrix and a list and perform
following.
(i) Update the element Sin the list.
(ii ) Merge two lists into one list
(iii). count number of object Sin a given list
Source Code:-
list_data<-list(c("HTML","CSS","Javascrpt","C++"),matrix(c(2,4,6,8), nrow = 2),list("Hello","Hi"))
print("List:")
print(list_data)
print("Update the third element of the list:")
list_data[3] = "R programming"
print("New list:")
print(list_data)
l1 = list(1, 2, 3,4,5)
l2 = list("white", "yellow", "pink")
print("Original lists:")
print(l1)
print(l2)
print("Merge the lists l1 and l2:")
mlist = c(l1, l2)
print("New merged list:")
print(mlist)
print("Number of objects in the list1")
print(length(l1))
print("Number of objects in the list2")
print(length(l2))
print("Number of objects in the merged list")
print(length(mlist))
R programming 7

OUTPUT-
R programming 8
R programming 9
R programming 10

Practical-3

AIM:- Write a program to check leap year in R.


Source Code:-
#input year is a leap year or not
year = as.integer(readline(prompt="Enter a year: "))
if((year %% 4) == 0) {
if((year %% 100) == 0) {
if((year %% 400) == 0)
{ print(paste(year,"is a leap year"))
} else {
print(paste(year,"is not a leap year"))
}
} else {
print(paste(year,"is a leap year"))
}
} else {
print(paste(year,"is not a leap year"))
}

OUTPUT:-
R programming 11

Practical-4

AIM:- Write a program to print the numbers from 1 to 15 in such a way that it prints "PUFF" for
multiples of 3, it prints "CHUG" for multiples of 5, and print "PUFFCHUG" for multiples of both
else it prints the number.
Source Code:-
for(i in 1:15){
if ((i %% 3 == 0) && (i %% 5 == 0))
{ print("PUFFCHUG")

}
else if (i %% 3 == 0)
{
print("PUFF")

}
else if (i %% 5 == 0)
{
print("CHUG")

}
else
print(i)
}
R programming 12

OUTPUT-
R programming 13

Practical-5

AIM:- Write a program to search a number in a list. If the number is found then print its position
else print -1.
Source Code:-
l<-list (20,30,45,54,56,76,88,90)
print("list is ")
print(l)
num<-readline("enter the nmuber to be searched")
num<-as.integer(num)
found<-FALSE
Position<-1
for (i in 1:8)
{
if(l [i]==num)
{
found<-TRUE
Position<-i
cat("position of the number is", i)
break
}
}
if (found == FALSE)
{
print(" number not found")
cat("poostion is" , Position)
}
R programming 14

OUTPUT-
R programming 15

Practical-6

AIM:- Create two lists to store information of two Students like Roll_No, Name, Age, Subjects.
Perform operations to print two lists, add new component Gender to two students, Access RollNo
and subjects of Student1, Access RollNo and subjects of Student2. Merge Subjects into new List
Subjects_All.
Source Code:-
print("Student1 info")
student1 <- list(rollno=1319107,name = "Nraj", age =21, subjects = c("C","c++"))
print(student1)
# Create a list of student information without named components
student2 <- list(1320132,"jigar", 19, c("R","PHP"))
# Use names() to give names to components of list
names(student2) <- c("rollno","name","age", "subjects")
print("Student2 info")
print(student2)
# Add Gender
print("2 students info after adding
gender") student1$Gender <- "Male"
student2["Gender"] <- "Male"
print(student1)
print(student2)
# $ operator is used to extract elements of List by literal names.
subjects_1 <- student1[["subjects"]]
cat("Student1's subjects ", subjects_1,"\n")
subjects_2 <- student2$subjects
cat("Student2's subjects ", subjects_2,"\n")
subjects_all <- append(subjects_1, subjects_2)
cat("All subjects ", subjects_all)
R programming 16

OUTPUT-
R programming 17

Practical-7

AIM:-Write a user defined function to generate Fibonacci series using while loop.
Source Code:-
# take input from the user
nterms = as.integer(readline(prompt="How many terms? "))
# first two terms
n1 = 0
n2 = 1
count = 2
# check if the number of terms is valid
if(nterms <= 0) {
print("Plese enter a positive integer")
} else {
if(nterms == 1)
{ print("Fibonacci
sequence:") print(n1)
} else {
print("Fibonacci sequence:")
print(n1)
print(n2)
while(count < nterms) {
nth = n1 + n2
print(nth)
# update values
n1 = n2
n2 = nth
count = count + 1
}
}
}
R programming 18

OUTPUT-
R programming 19

Practical-8

AIM:-Write a program in R to make a simple calculator.


Source Code:-
# Program make a simple calculator that can add, subtract, multiply and divide using
functions
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)
}
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(num1, num2), subtract(num1, num2), multiply(num1,
num2), divide(num1, num2))
print(paste(num1, operator, num2, "=", result))
R programming 20

OUTPUT-
R programming 21

Practical-9

AIM:- Write a R program to create an 3 dimensional array of 30 elements using the dim() function.

Source Code:-

# Create two vectors


data1 <- c(1,2,3,4,5,6)
data2 <- c(60, 18, 12, 13, 14, 19)
# assigning row names
row.names=c("row1","row2","row3")
# assigning column names
column.names=c("col1","col2","col3")
# assigning array names
matrix.names=c('array1','array2','array3')
# pass these vectors as input to the
array. # 3 rows,3 columns and 3 arrays
result <- array(c(data1, data2), dim = c(3,3,3),
dimnames=list(row.names,column.names, matrix.names))
print(result)
R programming 22

OUTPUT:-
R programming 23

Practical-10

AIM:- Write a R program to create two matrix and add, subtract, multiply and divide thee
matrices.
Source Code:-
# Create two 2x3 matrixes.
m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(m1)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(m2)
result = m1 + m2
print("Result of addition")
print(result)
result = m1 - m2
print("Result of subtraction")
print(result)
result = m1 * m2
print("Result of multiplication")
print(result)
result = m1 / m2
print("Result of division:")
print(result)
R programming 24

OUTPUT:-
R programming 25

You might also like