R program questions 1-24 (21)
R program questions 1-24 (21)
2 . when all the basic of ‘R’ programming (Data type, variables, operations)
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
Step – 1:- With R-base installed, let’s move on to installing R-Studio. To begin, Goto download
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:-
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
"C:/Program Files/R/R-3.2.2/library"
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.
Print (apple)
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)
# 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.
var.1=c(0,1,2,3)
# Assignment using leftwar operstor
var.2<- c("learn","R")
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
print(v&t)
#Assignment Operators
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.
Output :
# Creating a matrix
for (r in 1:nrow(mat))
for (c in 1:ncol(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.
cnt <- 2
{ print(v)
cnt = cnt + 1
Output :
4. write a program in ‘R’ to implement different library functions
Program Description :
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
Built-in Function
Sequence
print(seq(32,44))
main
print(mean(25:82))
Sum
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.
{for(i in 1:a)
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:
EXAMPLE :-
> 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
# 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
print(numbers)
#To create a vector with numerical values in a sequence, use the : operator:
print(numbers)
Vector Length
#To find out how many items a vector has, use the length() function:
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:-
Creating a matrix in R
*To create a matrix in ‘R’ you need to use the function called matrix()
syntax:-
example:-
print(s)
Multiplication
In R, the matrix multiplication command is %*%. Admittedly, this notation is a little
strange, but * is already taken for scalar multiplication.
7
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])
print(list[-1])
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)
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))
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.
factor(vector)
# create a factor
students gender <- factor(c("male", "female", "male", "transgender", "female"))
Output
In the above example, we have used the factor() function to create the factor named students gender.
# create a factor
print(students gender[1])
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
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
# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))
print(marital_status[1])
Output
[1] divorced
Levels: divorced married single
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.
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)
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
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. 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")
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.
• radius indicates the radius of the circle of the pie chart.(value between −1 and +1).
• clockwise is a logical value indicating if the slices are drawn clockwise or anti clockwise.
Input:-
Syntax:-
barplot(H, xlab, ylab, main, names.arg,col)
Following is the description of the parameters used −
Input:-
A <- c(17, 2, 8, 13, 1, 22)
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.
• 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.
Input:-
data <- data.frame(
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)
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 −
• type takes the value "p" to draw only the points, "l" to draw only the lines and "o" to draw bothpoints 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.
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)
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:-