0% found this document useful (1 vote)
543 views16 pages

R Programming Lab Manual

The R program takes name and age as input from the user using the readline() function, prints the input values along with a message, and also prints the version of the installed R using the R.version.string command. It demonstrates taking input from the user, storing in variables, printing the values along with a message, and getting the R version details.

Uploaded by

vivek gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (1 vote)
543 views16 pages

R Programming Lab Manual

The R program takes name and age as input from the user using the readline() function, prints the input values along with a message, and also prints the version of the installed R using the R.version.string command. It demonstrates taking input from the user, storing in variables, printing the values along with a message, and getting the R version details.

Uploaded by

vivek gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

INDORE INSTITUTE OF SCIENCE AND

TECHNOLOGY, INDORE

R Programming Lab Manual

2022-2023

Prepared by

Your Name:-
Enrolment No.:-
Class / Section :-

DEPARTMENT OF MECHANICAL ENGINEERING 1


Syllabus

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.

DEPARTMENT OF MECHANICAL ENGINEERING 2


INTRODUCTION OF R-PROGRAMMING
 R is a popular programming language used for statistical computing and graphical
representation.
 R programming is written by “Robert Gentleman” and “Ross Ihaka”.
 R is a free and open-source software.
 R programming is commonly used to solve statistics, Time Series, Classification and
other Data Science tasks.
 It is also preferred Data Visualization, become it has great packages.
 It provides many statistical techniques.
 It works on different platforms like windows, Linux, MAC
 It is a great resource for data analysis, data Visualization, data science and machine
Learning.
 FEATURES OF R: -
 It is an open-source programming language. Hence, you can install R for free.
 Non coders can also understand and perform programming in R as it is easy to
understand.
 R has various data structures and operators. It can be integrated with other programming
languages like C, C++, Java and Python.
 R consists of various inbuilt packages. This makes reporting the results of an analysis
easier by using R.
 R Comments: -

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 #.

Example: - 1. # This is R Comment

2. # 2nd CSE-1 Student.

 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 other programming language, It is common to use “=” as an assignment


operator. In R we can use both “=” and “” as assignment operator.
DEPARTMENT OF MECHANICAL ENGINEERING 3
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

We can use the “class( ) or typeof( )” function to check the datatype of a


variable.

DEPARTMENT OF MECHANICAL ENGINEERING 4


1. Vector: - To create a vector with more than one element, you should use c( ) function,
which means to combine the elements into a vector.
Example: - apple = c(“red”,”yellow”,”green”)
print(apple)
class(apple)

Output: - [1] “red” “yellow” “green”


[1] “character”
2. Lists: - It can contain many different types of elements inside it like vectors, functions
and even another list inside it.
Example: - list1 = list(c(2,5,3),21.3,sin)
Print(list1)
Output: - [[1]]
[1] 2 5 3
[[2]]
[1] 21.3
[[3]]
function(x).primitive(“sin”)
3. Matrices: - A matrix is a two-dimensional rectangular data set. It can be created using
a vector input to the matrix function.
Example: - # Create a matrix
M=matrix(c(‘a’,’b’,’c’,’d’,’e’,’f’),nrow=2,ncol=3,byrow=TRUE
Print(M)
Output: - [,1] [,2] [,3]
[1,] “a” “b” “c”
[2,] “d” “e” “f”
4. Arrays: - matrices are confined to two dimensions, arrays can be of any number of
dimensions. The array function takes a dim attribute which creates the required
number of dimension.
Example: - We create an array with two elements which are 3 x 3 matrices each.
# Create an array.
a = array(c(“green”,”yellow”),dim=c(3,3,2))
print(a)

DEPARTMENT OF MECHANICAL ENGINEERING 5


Output: -

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.

Factors are created using the factor( ) function.

Example: -

# Create a vector.

apple_colors=c(“green”,”green”,”yellow”,”red”,”red”,”red”,”green)

# Create a factor object.

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.

Data Frames are created using the data.frame( ) function.

DEPARTMENT OF MECHANICAL ENGINEERING 6


Example: -
# Create the data frame.
x=data.frame(gender=c(“Male”,”Male”,”Female”),height=c(152,171.5,165),w
eight=c(81,93,78),Age=c(42,38,26))
print(x)

Output: -

DEPARTMENT OF MECHANICAL ENGINEERING 7


1. AIM: - Write a R program to take input from the user and display the values. Also
print the version of R installation

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)

readline( ) function “to take input from the user”.


Prompt argument we can choose to display an appropriate message for the user.
(or)
a=readline(prompt = "Enter your name:")
b=readline(prompt = "Enter your age:")
c=readline(prompt = “Gender:”)
z=c(a,b,c)
print(z)
Output:-

2. AIM: - Write a R program to get the details of the objects in memory.


Program:
name="FLAT"

n1=25

n2=3.14

nums=c(1,2,3,4,5,6)

print(ls())

print("Details of the objects in memory:")

print(ls.str())

DEPARTMENT OF MECHANICAL ENGINEERING 8


Output: -

3. AIM:- Write an R program to create a sequence of numbers from 20 to 50 and find


the mean of numbers from 20 to 60 and sum of numbers from 51 to 61.

Program:
print("Sequence of numbers from 20 to 50")

print(seq(20,50))

print("Mean of numbers from 20 to 60:")

print(mean(20:60))

print("Sum of numbers from 51 to 91:")

print(sum(51:91))

Output: -

DEPARTMENT OF MECHANICAL ENGINEERING 9


4. AIM: - Write a R program to create a simple barplot on four subject marks.
Program:
Marks=c(60,70,83,99)
barplot(marks,main="Comparing marks of 4
subjects",xlab="subject",ylab="Marks",names.arg=c("Java","DBMS","Flat","PS"),col
="pink",Horiz=FALSE)

Output: -

5. AIM: - Write a R program to get the unique elements of a given string and unique
numbers of vector.

Program:

# Unique elements of given sting and numbers.


x="ALL ARE 2ND YEAR CSE STUDENTS: "
print("orginal vector:")
print(x)
tolower( ) means Translate characters in character
print("unique elements:") vectors, in particular from upper to lower case or vice
versa.
print(unique(tolower(x)))
a=c(1,2,3,2,1,4,5,3,6,8,4)
print("original vector:")
print(a)
print("unique number:")
print(unique(a))

DEPARTMENT OF MECHANICAL ENGINEERING 10


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:

# To Create a 3X3 Matrix

a=c(10,20,30) cbind( ) means to take a sequence of vector, matrix or


data-frame arguments and combine by columns or
b=c(11,21,31) rows, respectively.

c=c(12,22,32)

z=cbind(a,b,c)

print("To Diaplay the Matrix:")

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:

DEPARTMENT OF MECHANICAL ENGINEERING 11


7. AIM:- Write a R program to create a 5 x 4 matrix, 3 x 3 matrix with labels and fill
the matrix by rows and 3 x 3 matrix with labels and fill the matrix by columns.

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:

DEPARTMENT OF MECHANICAL ENGINEERING 12


8. AIM:- Write a R program to combine three arrays so that the first row of the first
array is followed by the first row of the second array and then first row of the third
array.
Program:
# Combine three arrays, Taking one row from each one by one
x=rbind(rep("A",3),rep("B",3),rep("C",3))
print(x)
y=rbind(rep("P",3),rep("Q",3),rep("R",3))
print(y)
z=rbind(rep("G",3),rep("H",3),rep("I",3))
print(z)
num=matrix(t(cbind(x,y,z)),ncol=3,byrow=TRUE)
print("combine three arrays:")
print(num)
(or)

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:

DEPARTMENT OF MECHANICAL ENGINEERING 13


9. AIM:- Write a R program to create a two-dimensional 5x3 array of sequence of even
integers greater than 50.

Program:

#5x3 array of sequence of even integers greater than 50

a=array(seq(from=50,length.out=15,by=2),c(5,3))

print("Content of the array:")

print("5x3 array of sequence of even integers greater than 50")

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: -

DEPARTMENT OF MECHANICAL ENGINEERING 14


11. AIM: - Write a R program to create an empty data frame.
Program:

# To Create an empty data frame


df =data.frame(matrix(nrow=0,ncol=0))
print(df)
Output: -

12. AIM: - Write a R program to create a data frame from four given vectors.
Program:

# To Crete a data frame from four given vectors.


name=c("Virat Kohli","KL Rahul","Rohit Sharma","Ishan Kishan","MS.Dhoni")
score=c(96,52,37,12,46)
boundaries=c(12,6,5,2,7)
wickets=c("Catch","LBW","Run Out","Stump","Catch")
print("Original data frame:")
print(name)
print(score)
print(boundaries)
print(wickets)
df=data.frame(name,score,boundaries,wickets)
print(df)
Output: -

DEPARTMENT OF MECHANICAL ENGINEERING 15


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:

# duplicated elements and unique numbers

a=c(10,20,10,40,30,50,20)

b=c(10,30,20,30,50,2,10)

print("Original data frame:")

z=data.frame(a,b)

print(z)

print("Duplicate Element:")

print(duplicated(z))

print("Unique numbers:")

print(unique(z))

Output: -

DEPARTMENT OF MECHANICAL ENGINEERING 16

You might also like