Professional Documents
Culture Documents
LIST OF
S.NO DATE GRADE REMARKS SIGNATURE
EXPRIEMENTS
Download and install
R-Programming
1 environment and install
basic packages using
install.packages()
command in R.
. Learn all the basics of
R-Programming (Data
2 types, Variables,
Operators etc.)
Implement R-Loops
3 with different
examples.
Implement different
data structures in R
5 (Vectors, Lists, Data
Frames)
Implementation of
6 vector data objects
operations
Implementation of
matrix, array and
7 factors and perform in
R
Implementation and
8 use of data frames in R
For example, knowing the data type for “Ross, Bob” will help a computer
know:
Variable:-
Operators:-
EXPERIMENT NUMBER- 3.
The structure of a Loop can be virtually divided into two parts, namely the control
statement, and the body. The control statement of a Loop comprises the conditions
that have to be met for the execution of the body of the Loop. For every iteration of
the Loop, the conditions in the control statement have to be true. The body of a Loop
comprises the block of code or the sequence of logical statements that are to be
executed multiple times. There are two types of Loops in Python, namely, For Loop,
and While Loop. When a Loop is written within another Loop, the control structure is
termed as a nested Loop.
In R programming, we require a control structure to run a block of code
multiple times. Loops come in the class of the most fundamental and strong
programming concepts. A loop is a control statement that allows multiple
executions of a statement or a set of statements. The word ‘looping’ means
cycling or iterating.
A loop asks a query, in the loop structure. If the answer to that query
requires an action, it will be executed. The same query is asked again and
again until further action is taken. Any time the query is asked in the loop, it
is known as an iteration of the loop. There are two components of a loop,
for (val in 1: 5)
# statement
print(val)
While Loop in R
It is a type of control statement which will run a statement or a set of
statements repeatedly unless the given condition becomes false. It is also an
entry controlled loop, in this loop the test condition is tested first, then the
body of the loop is executed, the loop body would not be executed if the test
condition is false.
while ( condition )
{
statement
}
While loop Flow Diagram:
Below are some programs to illustrate the use of the while loop in R
programming.
Example 1: Program to display numbers from 1 to 5 using while loop in R.
R
val = 1
# statements
print(val)
val = val + 1
Repeat Loop in R
It is a simple loop that will run the same statement or a group of statements
repeatedly until the stop condition has been encountered. Repeat loop does
not have any condition to terminate the loop, a programmer must specifically
place a condition within the loop’s body and use the declaration of a break
statement to terminate this loop. If no condition is present in the body of the
repeat loop then it will iterate infinitely.
repeat
{
statement
if( condition )
{
break
}
}
val = 1
repeat
# statements
print(val)
val = val + 1
if(val > 5)
break
EXPERIMENT NUMBER- 4
Functions are useful when you want to perform a certain task multiple times.
A function accepts input arguments and produces the output by executing
valid R commands that are inside the function. In R Programming Language
when you are creating a function the function name and the file in which you
are creating the function need not be the same and you can have one or
more function definitions in a single R file.
Types of function in R Language
Built-in Function: Built function R is sq(), mean(), max(), these
function are directly call in the program by users.
User-defined Function: R language allow us to write our own
function.
Note: In the above syntax f is the function name, this means that you are
creating a function with name f which takes certain arguments and executes
the following statements.
Built-in Function in R Programming Language
Here we will use built-in function like sum(), max() and min().
R
print(sum(4:6))
print(max(4:6))
print(min(4:6))
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
print(evenOdd(4))
print(evenOdd(3))
EXPERIMENT NUMBER- 5
EXPERIMENT NUMBER- 6
Vectors
X = c(1, 3, 5, 7, 8)
print(X)
EXPERIMENT NUMBER- 7
Syntax:
matrix(data, nrow, ncol, byrow)
Parameters:
data: contain a vector of similar data type elements.
nrow: number of rows.
ncol: number of columns.
byrow: By default matrices are in column-wise order. So this parameter
decides how to arrange the matrix
Example:-
A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
print(A)
EXPERIMENT NUMBER- 8
Data Frames
Data Frames are data displayed in a format as a table.
EXPERIMENT NUMBER- 9
Data Manipulation
Data manipulation involves modifying data to make it easier to read and to be more
organized. We manipulate data for analysis and visualization. It is also used with the
term ‘data exploration’ which involves organizing data using available sets of
variables.
At times, the data collection process done by machines involves a lot of errors and
inaccuracies in reading. Data manipulation is also used to remove these
inaccuracies and make data more accurate and precise.
For example:
We will use the default iris table in R, as follows:
#To load datasets package
library("datasets")
#To load iris dataset
data(iris)
summary(iris)
Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa: 50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 versicolor:0.300 versicolor:50
Median: 5.800 Median: 3.000 Median: 4.350 Median: 1.300 Virginica: 50
Mean: 5.843 Mean: 3.057 Mean: 3.758 Mean: 1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
EXPERIMENT NUMBER- 10
Control structures like if, while, and for allow you to control the flow of an R
program
Infinite loops should generally be avoided, even if (you believe) they are
theoretically correct.
Control structures mentioned here are primarily useful for writing programs; for
command-line interactive work, the “apply” functions are more useful.
pie(geeks, labels)
Output: