R Programming Lab Manual
R Programming Lab Manual
TECHNOLOGY, INDORE
2022-2023
Prepared by
Your Name:-
Enrolment No.:-
Class / Section :-
Page No
S. No Name of the Experiment
INTRODUCTION OF R-PROGRAMMING 3-7
Write a R program to take input from the user and display the
1. values. Also print the version of R installation. 8
2. Write a R program to get the details of the objects in memory. 8
Write an R program to create a sequence of numbers from 20 to
3. 50 and find the mean of numbers from 20 to 60 and sum of 9
numbers from 51 to 61.
Write a R program to create a simple barplot on four subject
4. marks. 10
Write a R program to get the unique elements of a given string
5. and unique numbers of vector. 10
Write a R program to create three vectors a, b, c with 3 integers.
6. Combine the three vectors to become a 3 x 3 matrix where each 11
column represents a vector. Print the content of the matrix.
Write a R program to create a 5 x 4 matrix, 3 x 3 matrix with
7. labels and fill the matrix by rows and 3 x 3 matrix with labels 12
and fill the matrix by columns.
Write a R program to combine three arrays so that the first row of
8. the first array is followed by the first row of the second array and 13
then first row of the third array.
Write a R program to create a two-dimensional 5x3 array of
9. sequence of even integers greater than 50. 14
Write a R program to create an array using four given columns,
10. three given rows, and two given tables and display the content of 14
the array.
11. Write a R program to create an empty data frame. 15
12. Write a R program to create a data frame from four given vectors. 15
Write a R program to create a data frame using two given vectors
13. and display the duplicated elements and unique rows of the said 16
data frame.
Comments can be used to explain R code, and to make it more readable. It can also be
used to prevent execution when testing alternative code.
Comments starts with a “#”. When executing code, R will ignore any thing that starts
with #.
Variables in R: -
Variables are used to store data with named locations that your programs can
manipulate.
A variable name can be a combination of letters, digits, period and underscore.
Integer --- 10L, 21L, 35L (where the letter “L” declares the integer.)
Character --- “mohan”
Numeric (or) double --- 10.5, 33.7
Logical --- TRUE or FALSE
Complex --- 9 + 3i (where “i” is the imaginary part).
Example: -
1. # To Declare Variable. 2. # To Declare Character / String.
x 37L z = “2nd Year CSE Students
class(x) class(z)
Output: - integer Output: - Character
5. Factors: - Factors are the r-objects which are created using a vector. It stores the
vector along with the distinct values of the elements in the vector as labels.
The labels are always character irrespective of whether it is numeric or character
or Boolean etc. in the input vector. They are useful in statistical modelling.
Example: -
# Create a vector.
apple_colors=c(“green”,”green”,”yellow”,”red”,”red”,”red”,”green)
factor_apple=factor(apple_colors)
6. Data Frames: - 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.
Output: -
Program:
# To take input from the user and display the values.
name=readline(prompt = "Enter your name:")
age=readline(prompt = "Enter your age:")
print(paste("My name is",name,"and i am",age,"years old."))
print(R.version.string)
n1=25
n2=3.14
nums=c(1,2,3,4,5,6)
print(ls())
print(ls.str())
Program:
print("Sequence of numbers from 20 to 50")
print(seq(20,50))
print(mean(20:60))
print(sum(51:91))
Output: -
Output: -
5. AIM: - Write a R program to get the unique elements of a given string and unique
numbers of vector.
Program:
6. AIM: - Write a R program to create three vectors a, b, c with 3 integers. Combine the
three vectors to become a 3 x 3 matrix where each column represents a vector. Print
the content of the matrix.
Program:
c=c(12,22,32)
z=cbind(a,b,c)
print(z)
(or)
Program:
m1=matrix(11:19,nrow=3,ncol=3,dimrows=list(rnames,cnames))
rnames=c(“r1”,”r2”,”r3”)
cnames=c(“c1”,”c2”,”c3”)
m1
Output:
Program:
# To Create a 5 X 4 Matrix.
m1=matrix(1:20,nrow=5,ncol=4)
print("5 x 4 matrix:")
dim means dimensions can be checked.
print(m1)
cells=c(1,2,3,4,5,6,7,8,9)
rnames=c("ROW1","ROW2","ROW3")
cnames=c("col1","col2","col3")
m2=matrix(cells,nrow=3,ncol=3,byrow=TRUE,dimnames = list(rnames,cnames))
print("3x3 matrix with labels filled by rows:")
print(m2)
print("3x3 matrix with labels filled by columns:")
m3=matrix(cells,nrow=3,ncol=3,byrow=FALSE,dimnames = list(rnames,cnames))
print(m3)
Output:
Program:
x=matrix(1:9,nrow=3,ncol=3)
x
y=matrix(11:19,nrow=3,ncol=3)
y
z=matrix(21:29,nrow=3,ncol=3)
z
m=matrix(t(cbind(x,y,z))ncol=3,byrow=TRUE)
“Combining theses arrays:”
m
Output:
Program:
a=array(seq(from=50,length.out=15,by=2),c(5,3))
print(a)
Output:
10. AIM: - Write a R program to create an array using four given columns, three given
rows, and two given tables and display the content of the array.
Program:
# To create an array
p=array(1:30,dim=c(3,5,2))
print(p)
Output: -
12. AIM: - Write a R program to create a data frame from four given vectors.
Program:
Program:
a=c(10,20,10,40,30,50,20)
b=c(10,30,20,30,50,2,10)
z=data.frame(a,b)
print(z)
print("Duplicate Element:")
print(duplicated(z))
print("Unique numbers:")
print(unique(z))
Output: -