[go: up one dir, main page]

0% found this document useful (0 votes)
35 views56 pages

R program questions 1-24 (21)

The document provides a comprehensive guide on using the R programming language, covering installation, basic programming concepts, and various functionalities such as loops, vectors, matrices, and data frames. It includes step-by-step instructions for installing R and R-Studio, as well as examples of arithmetic operations, data manipulation, and visualizations. Additionally, it highlights the significance of R in statistical analysis and data science.

Uploaded by

jsssdpavankumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views56 pages

R program questions 1-24 (21)

The document provides a comprehensive guide on using the R programming language, covering installation, basic programming concepts, and various functionalities such as loops, vectors, matrices, and data frames. It includes step-by-step instructions for installing R and R-Studio, as well as examples of arithmetic operations, data manipulation, and visualizations. Additionally, it highlights the significance of R in statistical analysis and data science.

Uploaded by

jsssdpavankumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

INDEX

1. Download and install ‘R’ programming environment and install basic


packages using install .(dot) packages () command in R.

2 . when all the basic of ‘R’ programming (Data type, variables, operations)

3. Implement ‘R’ loops with different example.

4. write a program in ‘R’ to implement different library functions

(Sequence, main, Sum)

5. write a ‘R’ program to implement arithmetic operation in vectors.

6) Write an ‘R’ program to implement operations on vectors.


7)write a ‘R’ program to implement 2*2 matrices and implement addition
,multiplication.
8)Write a ‘R’ program to implement to a list and list operations
9)write a ‘R’ program to implement data frame.

10)Write a ‘R’ program to handle null values in a data frame

11. Write a ‘R’ program to implement to factors and operations on factors.


12. Write a ‘R’ program to implement Arrays and different operation on
Array.
13. Write a ‘R’ program to read a String and different operation on String.
14. Write a ‘R’ program to read csv file and Handling the csv file and write
into csv files.
15. Write a ‘R’ program to read Excel file and Handling the Excel file and into
Excel file.
16.Write a ‘R’ program to implement pie chat for a given vector. And
implement the functionality.
17.Write a ‘R’ program to create bar chart of by taking data frame.
18.write a ‘R’ program to create box plot of by taking data frame implement
all functionality in a boxplot

19.write a ‘R’ program to create histogram of by taking data


frame implement to all the functionality in a histogram

20.write a ‘R’ program to create line chart of by taking data frame


implement to all functionality in a line chart

21.write a R program to create a scatter plot implement functions.


23.write a R program from CRAN and load packages to Library.
24.write a implement data reshaping
a.joining cols & rows of data frame
b.merging data frame
c.melting and casting
Brief Introduction of R Programming Language :

R is an open-source programming language that is widely used as a statistical software and


data analysis tool. R generally comes with the Command-line interface. R is available across
widely used platforms like Windows, Linux, and mac-OS. Also, the R programming language
is the latest Cutting –edge tools

It was designed by Ross Ihaka and Robert Gentleman at the University of Auckland,
New Zealand, and is currently developed by the R Development Core Team. R programming
language is an implementation of the S programming language. It also combines with lexical
scoping semantics inspired by Scheme. Moreover, the project conceives in 1992, with an
initial version released in 1995 and a stable beta version in 2000.

Use of R Programming :

⮚ It’s a platform-independent language. This means it can be applied to all operating system
⮚ It’s an open-source free language. That means anyone can install it in any organization
without purchasing a license
⮚ R programming is used as a leading tool for machine learning, statistics, and data analysis.
Objects, functions, and packages can easily be created by R.
⮚ R programming language is not only a statistic package but also allows us to integrate with
other languages (C, C++). Thus, can easily interact with many data sources and statistical
packages
⮚ The R programming language has a vast community of users and it’s growing day by day
⮚ R is currently one of the most requested programming languages in the Data Science job
market that makes it the hottest trend nowadays

HERE INSTALL THE R-STUDIO & PACKAGES IN WINDOWS


1. Download and install ‘R’ programming environment and install basic
packages using install .(dot) packages () command in R.
Installation of R-Studio on windows:

Step – 1:- With R-base installed, let’s move on to installing R-Studio. To begin, Goto download

R- Studio and click on the download button for R-Studio desktop

Step–2: Click on the link for the windows version of R-Studio and save the.exe file

Step–3: Run the .exe and follow the installation instructions. Click Next on the welcome window.
Enter/ browse the path to the installation folder and click Next to proceed. Select the folder for the
start menu shortcut or click on do not create shortcuts and then click Next. Wait for the installation
process to complete. Click Finish to end the installation.

Output :
Install the R Packages:-

❖ First, run R-Studio.


❖ After clicking on the packages tab, click on install. The following dialog box
will appear.
❖ In the Install Packages dialog, write the package name you want to install
under the Packages field and then click install. This will install the
packageyousearchedfororgiveyoualistofmatchingpackagesbasedonyour
package text.

Installing Packages:-

Loading Packages:-

Once the package is downloaded to your computer you can access the functions
and

Resources provided by the package in two different ways: #load the package to
use in the current R session

library (package name)

Getting Help on Packages:-

"C:/Program Files/R/R-3.2.2/library"

install. Packages("Package Name")

# Install the package named "XML". Install. Packages ("XML")


2 . when all the basic of ‘R’ programming (Data type, variables, operations)

Program Description :

Variables are nothing but reserved memory locations to store values. This means that,
when create a variable you reserve some space in memory.
A variable provides us with named storage that our programs can manipulate. A variable
in R can store an atomic vector, group of atomic vectors or a combination of many ‘R’
objects. A valid variable name consists of letters, numbers and the dot or underline
characters. The variable name starts with a letter or the dot not followed by a number.
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. R language is rich in built-in operators and provides following types of
operators.

Data Types :

Numeric :

v <-23.5

print(class(v))

Logical

v <- TRUE

print(class(v))

Integer v <-2L

print(class(v))
Output :

R-objects.
● Vectors
● Lists
● Matrices
● Arrays
● Factors
● Data Frames

Vectors
When you want to create vector with more than one element, you should use c() function
which means to combine the elements into a vector.

# Create a vector.

apple <- c(“red”,”blue”,”yellow”)

Print (apple)

# Get the class of the vector.

print(class(apple))
Output:-
Lists
A list is an R-object which can contain many different types of elements inside
it like vectors, functions and even another list inside it.

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# Print the list.


print(list1)
Output :
Matrices

A matrix is a two-dimensional rectangular data set. It can be created using a


vector input to the matrix function.

# Create a matrix.

M=matrix(c('a','a','b','c','b','a'),nrow=2,ncol=3,byrow=TRUE)

print(M)

Output :
Arrays
While 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. In the below example we create an array with two elements which are
3x3 matrices each.

# Create an array.
a<-array(c('green','yellow'),dim= c(3,3,2))

print(a)

Output :
Variables:

The variables can be assigned values using leftward, rightward and equal to operator.
The values of the variables can be printed using print() or cat() function. The cat()
function combines multiple items into a continuous print output.

# Assignment using equal operator.

var.1=c(0,1,2,3)
# Assignment using leftwar operstor

var.2<- c("learn","R")

# Assignment using rightward operator.

c(TRUE,1)->var.3

print(var.1)
cat ("var.1 is ",var.1,"\n")
cat ("var.2 is ",var.2,"\n")
cat ("var.3 is ",var.3,"\n")

Output :
R Operators :
Types of Operators

#Arithmetic Operators

v <- c( 2,5.5,6)

t <- c(8, 3, 4)

print(v+t)

#Relational Operators

v <- c(2,5.5,6,9)

t <- c(8,2.5,14,9)

print(v>t)

#Logical Operators

v <- c(3,1,TRUE,2+3i) t <- c(4,1,FALSE,2+3i)

print(v&t)

#Assignment Operators

v1 <- c(3,1,TRUE,2+3i) v2 <<- c(3,1,TRUE,2+3i) v3 = c(3,1,TRUE,2+3i)

print(v1) print(v2) print(v3)

Output :
3. Implement ‘R’ loops with different example.
Program Description :
A for loop is the most popular control flow statement. A for loop is used to iterate a vector.
It is similar to the while loop. There is only one difference between for and while, i.e., in
while loop, the condition is checked before the execution of the body, but in for loop
condition is checked after the execution of the body.

# Create fruit vector


fruit <- c('Apple', 'Orange',"Guava", 'Pinapple', 'Banana','Grapes')

# Create the for statement


for ( i in fruit){
print(i)
}

Output :
# Creating a matrix

mat <- matrix(data = seq(10, 21, by=1), nrow = 6, ncol =2)

# Creating the loop with r and c to iterate over the matrix

for (r in 1:nrow(mat))

for (c in 1:ncol(mat))

print(paste("mat[", r, ",",c, "]=", mat[r,c])) print(mat)

Output :
R while loop :

A while loop is a type of control flow statements which is used to iterate a block of code
several numbers of times. The while loop terminates when the value of the Boolean
expression will be false.

In while loop, firstly the condition will be checked and then after the body of the statement
will execute. In this statement, the condition will be checked n+1 time, rather than n times.

v <- c("Hello","while loop")

cnt <- 2

while (cnt < 7)

{ print(v)

cnt = cnt + 1

Output :
4. write a program in ‘R’ to implement different library functions

(Sequence, main, Sum)

Program Description :

A function is a set of statements organized together to perform a specific task. R has a


large number of in-built functions and the user can create their own functions.

In R, a function is an object so the R interpreter is able to pass control to the function, along
with arguments that may be necessary for the function to accomplish the actions.

The function in turn performs its task and returns control to the interpreter as well as any

result which may be stored in other objects.

Built-in Function

Sequence

# Create a sequence of numbers from 32 to 44

print(seq(32,44))

main

# Find mean of numbers from 25 to 82.

print(mean(25:82))

Sum

# Find sum of numbers frm 41 to 68.

print(sum(41:68))
Output :

User-defined Function
We can create user-defined functions in R. They are specific to what a user wants and once
created they can be used like the built-in functions. Below is an example of how a function is
created and used.

# Create a function to print squares of numbers in sequence.

new.function <- function(a)

{for(i in 1:a)

{ b <- i^2 print(b)}

# Call the function new.function supplying 6 as an argument.

new.function(6)
5. write a ‘R’ program to implement arithmetic operation in vectors.
To perform arithmetic operations on vectors in R, you can use standard operators (+, -, \*, /, ^, %)
directly on vectors, which R will apply element-wise. Here's a demonstration:

Explanation:

#Vector Creation:

Two vectors, vector1 and vector2, are created using the c() function.

#Arithmetic Operations:

The code demonstrates addition (+), subtraction (-), multiplication (*), division (/),
exponentiation (^), and modulus (%%) operations.

OPARAND MEANING

+ ADDITION

- SUBTRACTION

* MULTIPLICATION

/ DIVISION

^ EXPONENT

%% MODULUS

%/% INTEGER
DIVISION

#Element-wise Operations:

R automatically performs these operations on corresponding elements of the vectors,


resulting in a new vector containing the results.

● Output: The print() function displays the results of each operation.


Arithmetic operations of vectors are performed member-by-member,

EXAMPLE :-

we have two vectors a and b.

> a = c(1, 3, 5, 7)
> b = c(1, 2, 4, 8)

#ADDITION

And if we add a and b together, the sum would be a vector whose members are the
sum of the corresponding members from a and b.

>a+b

[1] 2 5 9 15

#subtraction
And if we subtract a and b together, the subtract would be a vector whose members
are the subtract (-) of the corresponding members from a and b.

>a-b

[1] 0 1 1 -1

#multiplication
And if we multiple a and b together, the multiple would be a vector whose members
are the multiple of the corresponding members from a and b.

>a*b

[1] 1 6 20 56

(or)
Then, if we multiply a by 5, we would get a vector with each of its members multiplied
by 5.
>5*a
[1] 5 15 25 35

#DIVISON
And if we divide a and b together, the divide would be a vector whose members are
the divide of the corresponding members from a and b.
>a/b

[1] 1.000 1.500 1.250 0.875

# EXPONENTIATION

the exponentiation operation calculates the power of one numeric variable raised to the power
of another.

>a^b
[1] 1 9 625 5764801

#MODULUS

the modulo operation is performed using the %% operator. It returns the remainder of
the division between two numbers

> a %% b

[1] 0 1 1 7

# INTEGER DIVISION

integer division, that is discards the fractional part, with the same effect as n %/% m . It can
be defined as floor(n/m) .

> a %/% b

[1] 1 1 1 0

6) Write an ‘R’ program to implement operations on vectors.


Vectors
A vector is simply a list of items that are of the same type.
To combine the list of items to a vector, use the c()
ex:-
x <- c("banana", "apple", "papaya")
print(x)

#In this example, we create a vector that combines numerical values:

numbers <- c(1, 2, 3)

print(numbers)

#To create a vector with numerical values in a sequence, use the : operator:

numbers <- 1:10

print(numbers)

Vector Length
#To find out how many items a vector has, use the length() function:

fruits <- c("banana", "apple", "papaya")

length(fruits)

Sort a Vector
#To sort items in a vector alphabetically or numerically, use
the sort() function:
fruits <- c("banana", "apple", "papaya", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)

sort(fruits)

sort(numbers)

Access Vectors
#You can access the vector items by referring to its index number inside
brackets []
fruits <- c("banana", "apple", "papaya")

fruits[1]

Repeat Vectors
To repeat vectors, use the rep()
repeat_each <- rep(c(1,2,3), each = 3)

print(repeat_each)
output:-

7.write a ‘R’ program to implement 2*2 matrices and implement


addition ,multiplication.

Matrix is a two dimensional arrangement of data in rows and columns

Creating a matrix in R
*To create a matrix in ‘R’ you need to use the function called matrix()
syntax:-

name<-matrix(data, nrow, ncol, by row, dimnames)

example:-

s<- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)

print(s)

Adding columns or rows


You will often want to add an additional row or column to your
matrix. rbind() and cbind() bind rows and columns onto existing matrices,
respectively.
m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
result = m1 + m2
print(result)

Multiplication
In R, the matrix multiplication command is %*%. Admittedly, this notation is a little
strange, but * is already taken for scalar multiplication.
7

m1 = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)


m2 = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
mul = m1 * m2
print(mul)
output:-
8)Write a ‘R’ program to implement to a list and list
operations

Lists
A list in R can contain many different data types inside it. A list is a collection
of data which is ordered and changeable.
To create a list, use the list()
Access Lists
You can access the list items by referring to its index number, inside
brackets.
list <- list(10,20,30,40,50)

print(list[1])

Remove List Items


You can also remove list items by using[-]
list <- list(10,20,30,40,50)

print(list[-1])

Add List Items


To add an item to the end of the list, use the append() function:
list <- list(10,20,30,40,50)

append(list,100 , after = 2)
output:-
9)write a ‘R’ program to implement data frame.
Data Frames
Data Frames are data displayed in a format as a table.
Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However,
each column should have the same type of data.
Use the data.frame() function to create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
print(Data_Frame)

Summarize the Data


Use the summary() function to summarize the data from a Data Frame
Syntax:-
print(summary(Data_Frame))
Structure the Data
we will use str() to find the structure of a data frame
Syntax:-
print(str(Data_Frame))
Access Items
We can use single brackets [ ], double brackets [[ ]] or $ to access columns
from a data frame:
Syntax:-
Data_Frame[1]

Data_Frame[["Training"]]

print(Data_Frame$Training)
Add Rows
Use the rbind() function to add new rows in a Data Frame
Syntax:-
New_row_DF <- rbind(Data_Frame, c("Strength", 110, 110))
print(New_row_DF)
Add Columns
Use the cbind() function to add new columns in a Data Frame
Syntax:-
New_col_DF <- cbind(Data_Frame, Steps = c(1000, 6000, 2000))
print(New_col_DF)
OUTPUT:-
10)Write a ‘R’ program to handle null values in a data frame
*Applying Na or NaN:
* missing values are those elements that are not present in the
continues data.
*Na or NaN are the reserved words that indicates a missing value.

*is.na()function:-
this returns a logical vector that indicate the all the null values(Na) present in
the data it contains Boolean values.
if Na is present in a vector this function returns TRUE else it returns false.
ex:-
v <-c(1, 2, NA, 4)
print(is.na(v))

Removing Na,NaN:
Extracting values except Na or Nan values
Syntax:-
v <-c(1, 2, NA, 4)
y<-is.na(v)
v[!v]

Which()function :-
The which function returns at which places the null values are present
Syntax:-
v <-c(1, 2, NA, 4)
which(is.na(v))
Sum() function:
This sum function returns total number of null values in a vector
Syntax:-
v <-c(1, 2, NA, 4)
Sum(is.na(v))

Filter()function:
These function are used to remove null values from data set
Syntax:
v <-c(1, 2, NA, 4)
print(na.omit(v))

11. Write a ‘R’ program to implement to factors and operations on factors.


R FACTORS
A Factor is a data structure that is used to work with categorizable datas.

Suppose a data field such as marital status may contain only values from single, married,
separated, divorced, or widowed.

In such a case, we know the possible values beforehand and these predefined, distinct
values are called levels of a factor.

Create a Factor in R
In R, we use the factor() function to create a factor. Once a factor is created, it can only
contain predefined set values called levels.

The syntax for creating a factor is

factor(vector)

Here, factor() takes a vector as an argument.

Let's see an example,

# create a factor
students gender <- factor(c("male", "female", "male", "transgender", "female"))

# print the marital_status factor

print (students gender)

Output

[1] male female male transgender female


Levels: female male transgender

In the above example, we have used the factor() function to create the factor named students gender.

Notice while printing students gender, we get two outputs

● All the vectors items


● predefined possible values we know beforehand i.e. levels of students gender

Access Factors Elements


Accessing vector elements is similar to that of vectors. We use the index number. For
example,

# create a factor

students gender <- factor(c("male", "female", "male", "transgender", "female"))

# access 1st element of students gender

print(students gender[1])

# access 4th element of students gender

print(students gender[4])

Output

[1] male
Levels: female male transgender
[1] transgender
Levels: female male transgender

In the above example, we have used the index number to access elements of the students_gender

● students_gender[1] - returns the 1st element of students_gender i.e "male"

● students_gender[4] - returns the 4th element of students_gender i.e. "transgender"

Note that each time we access and print factor elements we get a level of factor too.

Here, we have reassigned a new value to index 1 of the marital_status factor to change
the element

Modify Factor Element


To change a vector element, we can simply reassign a new value to the
specific index. For example

# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

# print the marital_status factor


marital_status[1] <- "divorced"hiuhtu

print(marital_status[1])

Output
[1] divorced
Levels: divorced married single

Here, we have reassigned a new value to index 1 of the marital_status factor to


change the element from "married" to "divorced".hiuhtu
12. Write a ‘R’ program to implement Arrays and different
operation on Array.
● Arrays are the data objects which allow us to store data in more than 1D.
● Array is created with a function called array()
● Array() takes a letter as an input and to create an array it uses vectors
values in the dim parameter.

Input:- #Vector Creation


a <- c(10, 20, 30, 40, 50)
print(a)
#Element Access
print(a[3])
#Element Modification
a[2] <- 25
print(a)
#Vector Expansion
a <- c(a, 60)
print(a)
#Element Removal
a <- a[-4]
print(a)
Finding Length
print(length(a))
#Finding Sum
print(sum(a))
#Finding Minimum value
print(min(a))
#Finding Maximum value
print(max(a))

Output:
______________________________________________
13. Write a ‘R’ program to read a String and different operation on
String.
Strings:
Any value written within a pair of single quote or double quotes in R is treated
as a string. Internally R stores every string within double quotes, even when
you create them with single quote.

Here's a concise explanation of the R string operations demonstrated:

● Basic String Info:


● nchar(): Finds the length of a string.
● Case Conversion:
● toupper(): Converts to uppercase.
● tolower(): Converts to lowercase.
● Substrings and Splitting:
● substr(): Extracts parts of a string.
● strsplit(): splits a string into a list of substrings, based on a
defined seperator.
● String Modification:
● gsub(): Replaces parts of a string.
● Pattern Matching:
● grepl(): Checks for the presence of a pattern.
● Stringr Package (for more advanced operations):
● str_count(): Counts pattern occurrences.
● str_extract(): Extracts matched patterns.
● str_extract() with regex: extracts the first word.
Input:-
str<-"Computer Applications"
print(str)
# Length
nchar(str)
# Uppercase
toupper(str)
# Lowercase
tolower(str)
# Substring (first 8 characters)
substr(str, 1, 8)
# Split into wordsstrsplit(str, " ")
# Replace "Computer" with "Software"
gsub("Computer", "Software", str)
#using stringr package
library(stringr)
# count occurrences of "a"
str_count(tolower(str), "a")
# extract the word Applications
str_extract(str, "Applications")
Output:-
14. Write a ‘R’ program to read csv file and Handling the csv file and
write into csv files.
CSV File:-
A Comma-Separated Values (CSV) file is a plain text file which contains a
list of data. These files are often used for the exchange of data between
different applications.For example, databases and contact managers mostly
support CSV files.
Reading a CSV file:-
data <- read.csv("record.csv")
print(data)
Handling the CSV file:-
To check the number of rows and number of columns with the help of nrow() and ncol()
function.
Example:- csv_data<- read.csv("record.csv")
print(is.data.frame(csv_data))
print(ncol(csv_data)) print(nrow(csv_data))

Writing into a CSV file:-


Like reading and analyzing, R also allows us to write into the .csv file. For this
purpose, R provides a write.csv() function. This function creates a CSV file from
an existing data frame. This function creates the file in the current working
directory. Let's see an example to understand how write.csv() function is used
to create an output CSV file.
Example:-
csv_data<- read.csv("record.csv")
#Getting details of those peoples who joined on or after 2014
details <- subset(csv_data,as.Date(start_date)>as.Date("2014-01-01"))
# Writing filtered data into a new file.
write.csv(details,"output.csv")
new_details<- read.csv("output.csv")
print(new_details)
15. Write a ‘R’ program to read Excel file and Handling the Excel file and into
Excel file.
To read, handle, and write Excel files in R, we typically use the readxl package
for reading data from Excel files and the writexl package (or openxlsx for more
complex manipulations) for writing data into Excel files.

Prerequisites:

To get started, you'll need to install the required packages. You can install them
using the following commands:
install.packages("readxl") # For reading Excel files
install.packages("writexl") # For writing Excel files

Once these packages are installed, load them into your R environment:
library(readxl)
library(writexl)

Example 1: Reading Data from an Excel File

We can use readxl::read_excel() to read data from Excel files. This function
supports both .xls and .xlsx formats.
# Reading data from an Excel file
data <- read_excel("your_file.xlsx")
# Print the first few rows of the data
head(data)

Handling Sheets:

If an Excel file contains multiple sheets, you can specify the sheet you want to
read by either using the sheet's name or index:
# Reading data from a specific sheet by name
data_sheet2 <- read_excel("your_file.xlsx", sheet = "Sheet2")
# Or by index (e.g., 1 for the first sheet)
data_sheet1 <- read_excel("your_file.xlsx", sheet = 1)
# Print first few rows
head(data_sheet2)
Example 2: Writing Data to an Excel File

The writexl::write_xlsx() function is used to write data frames or a list of


data frames to an Excel file.
# Create some data
df <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Gender = c("F", "M", "M")
)
# Write the data frame to an Excel file
write_xlsx(df, "output_file.xlsx")

Example 3: Handling and Modifying Excel Data

Once you have read the data into R, you can perform typical data manipulation
tasks such as filtering, transforming, and summarizing the data. Let’s consider
the data manipulation using the dplyr package (which is part of the tidyverse).

1. Filtering Data:
library(dplyr)
# Filter data where Age is greater than 30
filtered_data <- filter(data, Age > 30)
# Print filtered data
print(filtered_data)

0. Adding New Columns:


# Add a new column that categorizes Age as 'Young' or 'Old'
data <- mutate(data, Age_Group = ifelse(Age > 30, "Old", "Young"))
# Print the modified data
print(data)

0. Summarizing Data:
# Group by 'Gender' and calculate the average age
summary_data <- data %>%
group_by(Gender) %>%
summarize(Average_Age = mean(Age))
# Print summarized data
print(summary_data)
Example 4: Writing Modified Data Back to an Excel File

After manipulating or analyzing the data, you can write the updated data back
into a new Excel file.
# Write the modified data back to an Excel file
write_xlsx(data, "modified_output.xlsx")

Example 5: Using openxlsx for More Complex Operations

openxlsx is another useful package when you need more flexibility, such as
formatting cells, adding styles, or creating multiple sheets.
install.packages("openxlsx")
library(openxlsx)
# Create a new workbook
wb <- create Workbook()
# Add a worksheet
addWorksheet(wb, "Sheet1")
# Write data to the worksheet
writeData (wb, sheet = 1, x = data)
# Save the workbook to a file
saveWorkbook(wb, "complex_output.xlsx", overwrite = TRUE)

Explanation:

● Reading Data: We used read excel() to load data from an Excel file into
an R data frame.
● Writing Data: write_xlsx() was used to save the data frame into an Excel
file.
● Data Manipulation: We used dplyr functions to filter and modify the
data.
● Handling Multiple Sheets: We showed how to specify which sheet to read
from an Excel file when multiple sheets exist.
● Advanced Operations with openxlsx: openxlsx provides more control
over formatting and sheet management.
16.Write a ‘R’ program to implement pie chat for a given vector. And
implement the functionality.
Pie Chart:-
A pie-chart is a representation of values as slices of a circle with different
colors. The slices are labelled and the numbers corresponding to each slice is
also represented in the chart. In R the pie chart is created using the pie()
function which takes positive numbers as a vector input. The additional
parameters are used to control labels, color, title etc.

Syntax:- pie(x, labels, radius, main, col, clockwise)


Following is the description of the parameters used −

• x is a vector containing the numeric values used in the pie chart.

• labels is used to give description to the slices.

• radius indicates the radius of the circle of the pie chart.(value between −1 and +1).

• main indicates the title of the chart.

• col indicates the color palette.

• clockwise is a logical value indicating if the slices are drawn clockwise or anti clockwise.

Input:-

data_vector <- c(25, 15, 30, 20, 10)

labels <- c("A", "B", "C", "D", "E")

colors <- c("red", "blue", "green", "purple", "orange")

pie(data_vector, labels = labels, col = colors, main = "Pie Chart")


Output:-
17.Write a ‘R’ program to create bar chart of by taking data frame.
Bar chart:-
A bar chart represents data in rectangular bars with length of the bar proportional to the
value of the variable. R uses the function barplot() to create bar charts. R can draw both
vertical and horizontal bars in the bar chart.

Syntax:-
barplot(H, xlab, ylab, main, names.arg,col)
Following is the description of the parameters used −

• H is a vector or matrix containing numeric values used in bar chart.

• xlab is the label for x axis.

• ylab is the label for y axis.

• main is the title of the bar chart.

• col is used to give colors to the bars in the graph.

Input:-
A <- c(17, 2, 8, 13, 1, 22)

B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")

barplot(A, xlab ="Month", ylab ="Articles", col =rainbow(length(x)),main ="Bar chart")

Output:-
18.write a ‘R’ program to create box plot of by taking data frame implement
all functionality in a boxplot

Boxplots:-
Boxplots are a measure of how well distributed is the data in a data set. It
divides the data set into three quartiles. This graph represents the minimum,
maximum, median, first quartile and third quartile in the data set. It is also
useful in comparing the distribution of data across data sets by drawing
boxplots for each of them.

Syntax:-
Boxplot(x, data, notch, varwidth, names, main)
• x is a vector or a formula.

• data is the data frame.

• notch is a logical value. Set as TRUE to draw a notch.

• varwidth is a logical value. Set as true to draw width of the box proportionate to the sample
size.

• names are the group labels which will be printed under each boxplot.

• main is used to give a title to the graph.

Input:-
data <- data.frame(

Category = rep(c("A", "B", "C"), each = 20),

Value = c(rnorm(20, mean = 10, sd = 5),

rnorm(20, mean = 20, sd = 5),

rnorm(20, mean = 30, sd = 5))

boxplot(Value ~ Category, data = data,main = "Box Plot of Values by Category",

xlab = "Category",ylab = "Value",col = c("lightblue", "lightgreen", "lightcoral"),

border = "darkblue",notch = TRUE,outline = TRUE,varwidth = TRUE,horizontal = FALSE)


Output:-

19.write a ‘R’ program to create histogram of by taking data


frame implement to all the functionality in a histogram

Histograms:-
A histogram is a type of bar chart which shows the frequency of the number of
values which are compared with a set of values ranges. The histogram is used
for the distribution, whereas a bar chart is used for comparing different
entities. In the histogram, each bar represents the height of the number of
values present in the given range.
Syntax:-
hist(v,main,xlab,xlim,ylim,breaks,col,border)

• v is a vector containing numeric values used in histogram.

• main indicates title of the chart.

• col is used to set color of the bars.

• border is used to set border color of each bar.

• xlab is used to give description of x-axis.


• xlim is used to specify the range of values on the x-axis.

• ylim is used to specify the range of values on the y-axis.

• breaks is used to mention the width of each bar.

Input:-
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
hist(v, xlab = "No.of Articles ",main="Histogram",col
=rainbow(length(v)),border = "black")
Output:-
20.write a ‘R’ program to create line chart of by taking data frame
implement to all functionality in a line chart

Line Graphs:-
A line chart is a graph that connects a series of points by drawing line segments
between them. These points are ordered in one of their coordinate (usually the
x-coordinate) value. Line charts are usually used in identifying the trends in
data.
Syntax:-
plot(v,type,col,xlab,ylab)
Following is the description of the parameters used −

• v is a vector containing the numeric values.

• type takes the value "p" to draw only the points, "l" to draw only the lines and "o" to draw bothpoints and
lines.

• xlab is the label for x axis.

• ylab is the label for y axis.

• main is the Title of the chart.

• col is used to give colors to both the points and lines.

Input:-
R<-c(2,4,7,8,5,30,50,40)

plot(R,type='o',col="red",main="line graph",lwd=3)

Output:-
21.write a R program to create a scatter plot implement functions.
A Scatterplots show many points plotted in the Cartesian plane. Each point
represents the values of two variables. One variable is chosen in the horizontal
axis and another in the vertical axis.The simple scatterplot is created using the
plot() function.
Syntax:
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
Example input:-
v<-read.csv("cars.csv")
print(v)
print(v$wt)
print(v$mpg)
plot(x = input$wt,y = input$mpg, xlab = "Weight", ylab =
"Milage",
xlim = c(2.5,4),ylim =c(15,25),main = "scatter-plot")

output:-
23.write a R program from CRAN and load packages to

Library
The following command is used to get the packages directly from
CRAN webpage and install the package in the R environment. We
may be prompted to choose the nearest mirror. Choose the one
appropriate to our location. install.packages("Package Name")

Install package:-
To install a package manually, we first have to download it from.
https://cran.rproject.org/web/packages/available_packages_by_name.html. The
required package will be saved as a.zip file in a suitable location in the local
system.

Once the downloading has finished, we will use the following


command: install.packages(file_name_with_path, repos = NULL,
type = "source")

Once the downloading has finished, we will use the following


command:
install.packages(file_name_with_path, repos = NULL, type =
"source")
CRAN packages Description
Accurate, Adaptable, and Accessible Error Metrics for Predictive
1. A3 Models

Conditional Aalen-Johansen Estimation


2.
AalenJohansen
Reliability and Scoring Routines for the Approach-Avoidance Task
3. AATtools
Apps Based Activities for Communicating and Understanding
4. ABACUS Statistics

Coding 'ABA' Patterns for Sequence Data


5. abasequence
Readable String Abbreviation
6. abbreviate
Tools for Approximate Bayesian Computation (ABC)
7. abc
Data Only: Tools for Approximate Bayesian Computation (ABC)
8. abc.data
Array Based CpG Region Analysis Pipeline
9. ABC.RAP
Computed ABC Analysis
10. ABCanalysis

Load Package to Library:-


We cannot use the package in our code until it will not be
loaded into the current R environment.We also need to load a
package which is already installed previously but not
available in the current environment.There is the following
command to load a package:
library("package Name", lib.loc = "path to library")
24.write a implement data reshaping
a.joining cols & rows of data frame
b.merging data frame
c.melting and casting
A. Joining rows and columns in Data Frame:-
R allows us to join multiple vectors to create a data frame. For this
purpose R provides cbind() function.R also provides rbind() function,
which allows us to merge two data frame.There is the following
syntax of cbind() function and rbind() function. cbind(vector1,
vector2,.......vectorN) rbind(dataframe1, dataframe2,........dataframeN)
Let's see an example to understand how cbind() and rbind() function
is used.
Input:-
#Creating vector objects
Name <- c("Shubham Rastogi","Nishka Jain","Gunjan Garg","Sumit Chaudhary")

Address <- c("Moradabad","Etah","Sambhal","ravi")

Marks <- c(255,355,455,655)

#Combining vectors into one data frame


info <- cbind(Name,Address,Marks)
#Printing data frame
print(info)
# Creating another data frame with similar columns
new.stuinfo <- data.frame(
Name = c("Deepmala","Arun"),

Address = c("Khurja","Moradabad"),
Marks = c("755","855"),
stringsAsFactors=FALSE)

#Printing a header.
cat("# # # The Second data frame\n")
#Printing the data frame.
print(new.stuinfo)
# Combining rows form both the data frames.
all.info <- rbind(info,new.stuinfo)
# Printing a header.
cat("# # # The combined data frame\n")
# Printing the result.
print(all.info)
output:-
*Merging Data Frame:
R provides the merge() function to merge two data frames. In the merging
process, there is a constraint i.e.; data frames must have the same column
names.Let's take an example in which we take the dataset about Diabetes in
Pima Indian Women which is present in the "MASS" library. We will merge
two datasets on the basis of the value of the blood pressure and body mass
index.
Input:-
library(MASS)

merging_pima<- merge(x = Pima.te, y = Pima.tr,

by.x = c("bp", "bmi"),

by.y = c("bp", "bmi")

print(merging_pima)

nrow(merging_pima)

output:-
*Melting and Casting:-
In R, the most important and interesting topic is about changing the shape of the data in multiple steps
to get the desired shape. For this purpose, R provides melt() and cast() function. To understand its
process,consider a dataset called ships which is present in the MASS library.

Input:-

library(MASS)

print(ships)

output:-

You might also like