R20 Cse: R Programming Lab Manual
R20 Cse: R Programming Lab Manual
2022-2023
Prepared by
K.Vara Prasad
IInd CSE IInd Sem
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.
In INSTITUTE
AVANTHI other programming language,
OF ENGINEERING AND It is common to use “=” as an assignment
TECHNOLOGY 3
operator. In R we can use both “=” and “” as assignment operator.
Example: -1. name “siddhu”
Output: - “siddhu”
2. num1=10
num2=30
num = num1+num2
Output: - num = 30
R - Data Types: -
In contrast to other programming languages like C and Java in R, the variables are not
declared as some data type. The variables are assigned with R-Objects and the data type of
the R-object becomes the data type of the variable.
There are many types of R-objects. The frequently used ones are:
Vectors
Lists
Matrices
Array
Factors
Data Frames
Basic data types in R can be divided into the following types.
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.str())
Output: -
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:
Output:
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)
Program:
a=array(seq(from=50,length.out=15,by=2),c(5,3))
print(a)
Output:
# 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:
13. AIM: - Write a R program to create a data frame using two given vectors and
display the duplicated elements and unique rows of the said data frame.
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: -