0% found this document useful (0 votes)
7 views

Vinit R Programming

Uploaded by

20220702146
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Vinit R Programming

Uploaded by

20220702146
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

LABORATORY MANUAL

21BTCS35C05 R Programming Laboratory

Submitted By

Vinit Koshti
(Reg. No. 20220702139)

BACHELOR OF TECHNOLOGY

In

COMPUTER SCIENCE ENGINEERING

UNITEDWORLD INSTITUTE OF TECHNOLOGY (UIT)

KARNAVATI UNIVERSITY
Uvarsad, Gandhinagar, Gujarat-382422

DECEMBER 2024
Experiment-
1 Download and install R-Programming environment and install
basic packages using install. Packages () command in R.
Date: 05-08-
2024

Aim: Download and install R-Programming environment and install basic


packages using install. Packages () command in R.
Theory:

R is a programming language primarily used for statistical computing and


data analysis. It is open-source and widely used in academic, data
science, and machine learning fields. After installing R, you can install
additional packages to extend its functionality. Packages in R are
collections of functions and data sets that can be installed from CRAN
(Comprehensive R Archive Network) using the install. Packages ()
function.

Steps for Downloading, Installing, and Setting Up R:

1. Download R:
o Go to the official R website: https://cran.r-project.org/
o Choose your operating system (Windows, macOS, or Linux).
o Download and run the appropriate installer for your system.

2. Install R:
o Run the downloaded installer and follow the installation instructions.
o Once installed, R will be available on your system.

3. Install RStudio (optional but recommended):


o Download and install RStudio from https://www.rstudio.com/.
o RStudio is an Integrated Development Environment (IDE) that
provides a user-friendly interface to write, run, and manage R code.

Steps to Install Packages in R:

1. Open R or RStudio.
2. Use install.packages () to install packages.

Program:
Result: Rstudio install successfully.

Summary of experiment: Download and install R-Programming


environment and install basic packages using install. Packages ()
command in R was successfully don

Experiment-
2 Learn all the basics of R-Programming (Datatypes, Variables)

Date: 05-08-
2024

Aim: Implementation and perform the various basic operations in R

Theory:

R is a programming language and environment commonly used for


statistical computing and data analysis. R allows for a variety of data
types and structures, including vectors, lists, matrices, data frames, and
factors. Understanding these basic data types and how to manipulate
them is essential for any R programmer.

Algorithm:

1. Start R environment.
2. Create variables of different data types:
o Numeric
o Character
o Logical
3. Perform operations on these variables.
4. Create vectors, lists, and data frames.
5. Access and manipulate the elements of these structures.
6. End the R session.
Program:

Result:
Summary of the Experiment:
The various operations on data frames in R has been implemented and
successfully verified

Experiment-
3 To perform Basic Mathematical Operations in R
Date: 05-08-
2024

Aim: Implementation and perform the Mathematical operations in R

Theory:

R is equipped with various functions and operators to perform basic


mathematical operations. These include addition, subtraction,
multiplication, division, exponentiation, and more. Understanding these
operations is fundamental for performing statistical analysis and data
manipulation.

Algorithm:

1. Start R environment.
2. Define numeric variables.
3. Perform addition, subtraction, multiplication, and division
operations.
4. Use mathematical functions like sqrt(), log(), exp().
5. End the R session.

Program:
Result:
Summary of the Experiment:
The various Mathematical operations in R has been implemented and
successfully verified

Experiment-
4 To Perform various operators in R
Date: 05-08-
2024

Aim: Implementation and perform the various operators in R

Theory:

Operators in R include arithmetic, relational, logical, and assignment


operators. These operators are essential for performing calculations,
making comparisons, and controlling the flow of the program.

Algorithm:

1. Start R environment.
2. Define variables to use with operators.
3. Perform arithmetic operations (+, -, *, /).
4. Use relational operators (>, <, ==, !, =).
5. Apply logical operators (&, | , !).
6. Use assignment operators (=, <-).
7. End the R session.

Program:

Result:
Summary of the Experiment:
The various operations in R has been implemented and successfully
verified

Experiment- Write a program to check whether a year (integer) entered by


5 the user is a leap year or not?
Date: 05-08-
2024

AIM: Write a program to check whether a year (integer) entered by the user
is a leap year or not?
THEORY:
A leap year is a year divisible by 4 but not by 100, unless it's also divisible
by 400. This means:

 If a year is divisible by 400, it is a leap year.


 If a year is divisible by 100 but not by 400, it's not a leap year.
 If a year is divisible by 4 but not by 100, it is a leap year.
 All other years are not leap years.

ALGORITHM:

1. Input the year.


2. Check if the year is divisible by 400:
 If true, it is a leap year.

3. Else, check if the year is divisible by 100:

 If true, it is not a leap year.

4. Else, check if the year is divisible by 4:

 If true, it is a leap year.

5. Else, it is not a leap year.

6. Output the result.

PROGRAM:

RESULT:

Summary of the Experiment: a program to check whether a year


(integer) entered by the user is a leap year or not is successfully executed.
Experiment-
6 Implementation of Matrix, List in R Programming
Date: 05-08-
2024

Aim: Implementation and perform the various operations on data frames


in R

Theory:

Matrices and lists are fundamental data structures in R. A matrix is a two-


dimensional array where elements are arranged in rows and columns. A
list is a collection of elements that can be of different types and lengths.
Understanding these structures is crucial for data manipulation and
analysis.

Algorithm:

1. Start R environment.
2. Create a matrix using matrix() function.
3. Perform operations on the matrix (e.g., addition, multiplication).
4. Create a list using list() function.
5. Access and manipulate list elements.
6. End the R session.

Program:
Result:
Summary of the Experiment:
The various operations in R has been implemented and successfully
verified
Experiment-
7 Implementation of Array, Factors in R Programming
Date: 05-08-
2024

Aim: Implementation and perform the various operations on data frames


in R

Theory:

Arrays in R are multi-dimensional generalizations of vectors and matrices.


Factors are used to handle categorical data, storing it as levels. Both are
essential for data analysis, especially when dealing with structured data.

Algorithm:

1. Start R environment.
2. Create an array using array() function.
3. Perform operations on the array.
4. Create factors using factor() function.
5. Manipulate and analyze factors.
6. End the R session.

Program:
Result:

Summary of the Experiment:


The various operations in R has been implemented and successfully
verified
Experiment-
8 Study and implementation of data transpose operations in R
Date: 05-08-
2024

Aim: Study and implementation of data transpose operations in R

Theory:
A Data Frame in R is a two-dimensional table-like structure where each
column contains values of one variable, and each row contains a set of
values from each column. It allows for easy manipulation of tabular data
by performing operations like adding/removing columns, filtering rows,
sorting, and applying functions across rows/columns.
Algorithm Steps:
 Create a Data Frame using data.frame () with vectors as input.
 Access data by referencing rows/columns via $, [], or column names.
 Add or remove columns by assigning values to new columns or
setting columns to NULL.
 Filter rows using logical conditions on the data frame.
 Sort the data by using the order () function on a column.
 Merge data frames using merge () on common keys.
 Apply functions to rows/columns with apply () or get a summary of the
dat.

Program:
r

Result: All the operation run successfully.

Summary of the Experiment: The various operations on data frames in


R has been implemented and successfully verified
Experiment-
9 To Create Sample (Dummy) Data in R and perform data
Date: 05-08- manipulation with R
2024

Aim: To Create Sample (Dummy) Data in R and perform data


manipulation with R

Theory:
Dummy data, or sample data, is artificially generated data used for
testing, development, and learning purposes. In R, this can be done using
built-in functions like data.frame(), rep(), and sample(). Data manipulation
includes filtering rows based on conditions, sorting data, modifying or
adding columns, and summarizing the dataset for analysis. These
operations are essential for handling real-world data for statistical
modeling and visualization.

Algorithm Steps:

1. Create Dummy Data:

 Use data.frame () to create a data frame.


 Use functions like sample (), seq (), and rep () to generate random or
structured data.

2. Access Data:

 Retrieve specific rows or columns using indexing ( [] or $).

3. Manipulate Data:

 Add/Modify Columns: Assign new values to a new or existing


column.
 Filter Data: Subset the data frame based on conditions.
 Sort Data: Sort data using order () on specific columns.

4. Summarize Data:

 Use functions like summary (), mean (), max (), min () to summarize data.

5. View Results:

 Output the manipulated data frame and view the summary


statistics.
Program:
Result:

Summary of the Experiment: Create a Dummy data and successfully


perform data manipulation.
Experiment-
10 Implementation and perform the various operations on data
Date: 05-08- frames in R
2024

Aim: Implementation and perform the various operations on data frames


in R.

Theory: A data frame in R is a table-like structure where each column


can have different data types, making it ideal for handling structured
data. R provides several functions for data frame manipulation, including
adding/removing columns, filtering rows based on conditions, sorting data,
and summarizing the dataset. These operations are key to preparing and
analysing real-world datasets in statistical and data science tasks.

Algorithm:
1. Create a Data Frame: Use data.frame () to create a data frame with
columns of data.
2. Access Data:

 Access rows and columns using $, [], or column names.

3. Manipulate Data:

 Add/Modify Columns: Use $ to add new columns or update


existing ones.
 Filter Data: Subset data based on conditions using [].
 Sort Data: Sort rows using the order () function.

4. Summarize Data: Use summary (), mean(), etc., to generate summary


statistics.

5. Display Results: Output the manipulated data for viewing.

Program:
Result:

Summary of the Experiment: Various Operation on DataFrame are


successfully implemented.
Experiment-
11 Implementation and perform the various operations on data
Date: 05-08- frames in R
2024

Experiment- Study and implementation of various control structures in R


12
Date: 05-08-
2024

Aim: Study and implementation of various control structures in R.

Theory:

Control structures in R allow you to dictate the flow of execution in a


program. The main control structures are:

 Conditional Statements (if, else if, else) to make decisions based


on conditions.
 Loops (for, while, repeat) for iterating over data or executing code
multiple times.
 Break and Next for controlling loop flow (exiting or skipping
iterations). These structures are essential for controlling how your
program behaves based on various inputs and conditions.
Algorithm:

1. Use Conditional Statements:


o Check conditions with if, else if, and else to execute code based on
logical expressions.

2. For Loop:
o Loop over a sequence of values using for.

3. While Loop:
o Execute code repeatedly as long as a condition is TRUE using while.

4. Repeat Loop:
o Repeat code indefinitely until a break condition is encountered.

5. Control Loop Execution:


o Use break to exit a loop early.
o Use next to skip to the next iteration.

Program:
Result: All the control structure successfully run.

Summary of Experiment: Implementation of various control structure in


R is successfully implemented.

Experiment- Data Manipulation with dplyr package


13
Date: 05-08-
2024

Aim: Data Manipulation with dplyr package.

Theory:
The dplyr package in R is designed for fast and easy data manipulation
tasks. It provides a set of functions (often referred to as "verbs") such as
filter (), select (), arrange (), mutate (), summarize (), and group by().
These functions allow for powerful, efficient, and readable data
manipulation. dplyr often uses the pipe operator (%> %) to chain
multiple operations together, improving readability and making the code
concise.

Algorithm:

1. Load the dplyr package using library (dplyr).


2. Create a data frame or use an existing dataset.
3. Use the filter () function to filter rows based on conditions.
4. Use the select () function to choose specific columns.
5. Use the arrange () function to sort rows based on a column.
6. Use the mutate () function to add or modify columns.
7. Use the summarize () function to generate summary statistics.
8. Use the group_by () function to group data for grouped operations.
9. View the results after each step of manipulation.

Program:
Result: All the data manipulation done successfully.
Summary of Experiment: Data Manipulation with dplyr package
executed successfully.

Experiment- Data Manipulation with data.table package


14
Date: 05-08-
2024

Aim: Data Manipulation with data.table package

Theory: The data.table package in R is optimized for handling large


datasets with fast and memory-efficient operations. It extends data.frame
by providing syntax for fast aggregation, indexing, and subsetting. The
main advantage of data.table is its ability to process data quickly, especially
when working with large datasets. The syntax uses DT[i, j, by], where:

 i is for row filtering (subset),


 j is for column operations (select, mutate),
 By is for grouping data.

Algorithm:

1. Load the data.table package using library (data.table).


2. Create or convert a data frame into a data table using setDT ().
3. Filter rows by specifying conditions in the i argument.
4. Select columns or perform column operations using the j argument.
5. Sort data using the order () function.
6. Group data using the by argument and summarize data.
7. View results after each operation.

Program:
Result: All the data manipulation done successfully.

Summary of Experiment: Data Manipulation with dplyr package


executed successfully.

Experiment- Study and implementation of Data Visualization with


15 ggplot2
Date: 30-09-
2024
Aim : Study and implementation of Data Visualization with ggplot2
Theory : ggplot2 is a powerful R package for creating elegant and
informative data visualizations. It follows a grammar of graphics
approach, allowing you to build plots layer by layer.
Key Components:
 Aesthetic Mappings: Define how variables are mapped to visual
properties (x-axis, y-axis, color, size, shape).
 Geometric Objects: Visual elements like points, lines, bars, text.
 Faceting: Create subplots based on different categories.

Algorithm :
 Load the ggplot2 package
 Create a data frame
 Create a basic plot
 Add geometric objects
 Customize the plot
 Facet the plot
 Display the plot
Program :
Result: All the data manipulation done successfully.

Summary of Experiment: Study and implementation of Data


Visualization with ggplot2 executed successfully

You might also like