R Programming Practical File (1319036)
R Programming Practical File (1319036)
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
INDEX
Practical-1
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
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
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:-
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