0% found this document useful (0 votes)
13 views78 pages

Introduction to R Programming Basics

The document provides an introduction to the R Programming Language, highlighting its features, applications, and installation process. R is a powerful open-source tool for statistical computing and data analysis, widely used across various industries for tasks such as data visualization, machine learning, and financial modeling. It supports extensive libraries and integration with other programming languages, making it a versatile choice for data-driven professionals.

Uploaded by

sucheta
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)
13 views78 pages

Introduction to R Programming Basics

The document provides an introduction to the R Programming Language, highlighting its features, applications, and installation process. R is a powerful open-source tool for statistical computing and data analysis, widely used across various industries for tasks such as data visualization, machine learning, and financial modeling. It supports extensive libraries and integration with other programming languages, making it a versatile choice for data-driven professionals.

Uploaded by

sucheta
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

Introduction to RR-Programming

Introduction
The R Language stands out as a powerful tool in the modern era of statistical
computing and data analysis. Widely embraced by statisticians, data scientists, and
researchers, the R Language offers an extensive suite of packages and libraries tailored
for data manipulation, statistical modeling, and visualization. In this article, we explore
the features, benefits, and applications of the R Programming Language, shedding light
on why it has become an indispensable asset for data-driven professionals across
various industries.

R programming language is an implementation of the S programming language. It also


combines with lexical scoping semantics inspired by Scheme. Moreover, the project was
conceived in 1992, with an initial version released in 1995 and a stable beta version in
2000.

What is R Programming Language?


R programming is a leading tool for machine learning, statistics, and data analysis,
allowing for the easy creation of objects, functions, and packages. Designed by Ross
Ihaka and Robert Gentleman at the University of Auckland and developed by the R
Development Core Team, R Language is platform-independent and open-source,
making it accessible for use across all operating systems without licensing costs.
Beyond its capabilities as a statistical package, R integrates with other languages like C
and C++, facilitating interaction with various data sources and statistical tools. With a
growing community of users and high demand in the Data Science job market, R is one
of the most sought-after programming languages today. Originating as an
implementation of the S programming language with influences from Scheme, R has
evolved since its conception in 1992, with its first stable beta version released in 2000.

When was R created?


R was created in the early 1990s by University of Auckland statisticians Ross Ihaka and
Robert Gentleman. Ihaka and Gentleman, both then statistics professors at the New
Zealand university, saw what Ihaka called a “common need for a better software
environment” in their computer science laboratories. This realization prompted the pair
to begin developing R, an implementation of the earlier S programming language.

Although the professors started working on R in the early 90s, version 1.0.0 wasn’t
officially released until February 2000.
Features of R Programming Language
The R Language is renowned for its extensive features that make it a powerful tool for
data analysis, statistical computing, and visualization. Here are some of the key features
of R:

1. Comprehensive Statistical Analysis:


R langauge provides a wide array of statistical techniques, including linear and
nonlinear modeling, classical statistical tests, time-series analysis, classification,
and clustering.

2. Advanced Data Visualization:


With packages like ggplot2, plotly, and lattice, R excels at creating complex and
aesthetically pleasing data visualizations, including plots, graphs, and charts.

3. Extensive Packages and Libraries:


The Comprehensive R Archive Network (CRAN) hosts thousands of packages that
extend R’s capabilities in areas such as machine learning, data manipulation,
bioinformatics, and more.

4. Open Source and Free:


R is free to download and use, making it accessible to everyone. Its open-source
nature encourages community contributions and continuous improvement.

5. Platform Independence:
R is platform-independent, running on various operating systems, including
Windows, macOS, and Linux, which ensures flexibility and ease of use across
different environments.

6. Integration with Other Languages:


R language can integrate with other programming languages such as C, C++,
Python, Java, and SQL, allowing for seamless interaction with various data sources
and computational processes.

7. Powerful Data Handling and Storage:


R efficiently handles and stores data, supporting various data types and structures,
including vectors, matrices, data frames, and lists.

8. Robust Community and Support:


R has a vibrant and active community that provides extensive support through
forums, mailing lists, and online resources, contributing to its rich ecosystem of
packages and documentation.
9. Interactive Development Environment (IDE):
RStudio, the most popular IDE for R, offers a user-friendly interface with features
like syntax highlighting, code completion, and integrated tools for plotting, history,
and debugging.

10. Reproducible Research:


R supports reproducible research practices with tools like R Markdown and Knitr,
enabling users to create dynamic reports, presentations, and documents that
combine code, text, and visualizations.

Applications of R language
We use R for Data Science. It gives us a broad variety of libraries related to
statistics. It also provides the environment for statistical computing and design.
R is used by many quantitative analysts as its programming tool. Thus, it helps in
data importing and cleaning.
R is the most prevalent language. So many data analysts and research
programmers use it. Hence, it is used as a fundamental tool for finance.
Tech giants like Google, Facebook, Bing, Twitter, Accenture, Wipro, and many more
using R nowadays.

How to Download R and R Studio?


To Install R and R Studio on Windows we will have to download R and R Studio with
the following steps.

Step 1: First, you need to set up an R environment in your local machine. You can
download the same from [Link].
Install R and R Studio

You have to download both the applications first go with R Base and then install
RStudio. after click on install R you will get a new page like this.
Install R and R Studio

Here we can select the linux,mac or windows any one according to users system. you
have to click on for which you want to install.

Install R and R Studio

now click on the link show above in image so R base start downloading and after again
go to main page and download and click on Install RStudio.

Steps to Install R and R Studio


Step 1: After downloading R for the Windows platform, install it by double-clicking it.
Step 2: Download R Studio from their official page. Note: It is free of cost (under AGPL
licensing).

Step 3: After downloading, you will get a file named “[Link]” in your
Downloads folder.

Step 4: Double-click the installer, and install the software.

Step 5: Test the R Studio installation

Search for RStudio in the Window search bar on Taskbar.


Start the application.
Insert the following code in the console.
Input : print('Hello world!') Output : [1] "Hello world!"
Step 6: Your installation is successful.

Real world uses of R


R is a powerful programming language and environment widely used for statistical
computing, data analysis, and machine learning. It has many real-world applications
across various industries. Here are some key uses of R:

1. Data Analysis and Visualization


Data Manipulation: R is used for cleaning, transforming, and preparing data for
analysis. Libraries like dplyr and tidyr make data manipulation easier.
Statistical Analysis: It provides extensive statistical techniques like linear
regression, ANOVA, hypothesis testing, and time-series analysis.
Data Visualization: R has powerful packages such as ggplot2 for creating high-
quality static, dynamic, and interactive visualizations.
Business Analytics: Companies use R to analyze sales data, customer behavior,
market trends, and other business-related metrics.

2. Healthcare and Pharmaceuticals


Medical Research: R is used for analyzing clinical trials, bioinformatics, and
epidemiology studies. It helps in analyzing patient data, treatment outcomes, and in
predictive modeling.
Genomic Data Analysis: In genomics, R is used for analyzing gene expression, DNA
sequencing, and other bioinformatics applications.
Healthcare Predictions: Predictive models for patient outcomes, disease outbreaks,
and healthcare planning are built using R.
3. Finance and Economics
Financial Modeling: R is used for building quantitative models in finance, such as
risk analysis, portfolio optimization, and asset pricing.
Risk Management: R can be used to measure financial risk, conduct stress testing,
and model complex financial systems.
Economic Forecasting: R is widely used in economic research and policy planning
to model and forecast economic variables such as GDP, inflation, and
unemployment.

4. Marketing and Customer Analytics


Customer Segmentation: R is used for analyzing customer data, segmenting
customers into different groups based on behavior and demographics.
Sentiment Analysis: With text mining and natural language processing (NLP) tools
like tm and text, R is used to analyze customer sentiment from social media,
reviews, or surveys.
Churn Prediction: Businesses use R to predict which customers are most likely to
leave (churn) based on historical data.

5. Machine Learning and Artificial Intelligence


Predictive Modeling: R has a rich ecosystem of machine learning packages like
caret, randomForest, xgboost, and keras for training predictive models.
Classification and Clustering: R is used for classification tasks (e.g., spam
detection, fraud detection) and clustering tasks (e.g., market segmentation).
Deep Learning: R can interface with deep learning frameworks like TensorFlow and
Keras to develop neural networks for tasks like image recognition and natural
language processing.

6. Education and Research


Statistical Education: R is widely used in academia to teach statistics and data
science, as it provides a comprehensive suite of statistical tools.
Research Data Analysis: Researchers in fields like economics, psychology, and
sociology use R to analyze data collected through surveys, experiments, and
observational studies.

7. Government and Public Policy


Public Health Monitoring: R is used by governments and public health agencies to
monitor diseases, model public health outcomes, and allocate resources effectively.
Policy Analysis: Governments use R to analyze demographic trends, assess the
impact of policies, and forecast future needs for education, healthcare, or
infrastructure.
Census Data Analysis: R is often used to analyze large-scale census and survey
data to understand population dynamics.

8. Environmental Science
Climate Modeling: R is used to model climate change scenarios, analyze
temperature trends, and predict future climate conditions.
Ecology and Conservation: R helps analyze biodiversity, population dynamics, and
environmental conservation data.
Sustainability Research: Environmental agencies use R to assess sustainable
resource usage and track environmental impacts.

9. Sports Analytics
Performance Analysis: Teams use R for analyzing player statistics, improving
strategies, and optimizing performance in various sports.
Game Prediction Models: R is used to predict outcomes of sports events based on
historical data, player performance, and other variables.

10. Social Sciences and Psychology


Survey Analysis: R is frequently used to analyze survey data, including market
research, opinion polls, and social science studies.
Behavioral Studies: Psychologists and sociologists use R to analyze patterns in
behavior and conduct experiments.

11. Supply Chain and Operations


Logistics Optimization: Companies use R to optimize supply chain processes, such
as route planning, inventory management, and demand forecasting.
Production Planning: R helps in production scheduling, process optimization, and
workforce allocation.

12. Web Analytics


Website Traffic Analysis: R is used to analyze web traffic data to understand user
behavior, optimize content, and improve user experience.
A/B Testing: R is commonly used to analyze A/B test results and determine which
versions of a website or ad campaign perform better.

R's extensive libraries, rich functionality, and integration capabilities with other tools
make it a versatile language for a wide range of professional fields. Its open-source
nature also fosters innovation and collaboration across different industries.

Variables in R
A variable is a memory allocated for the storage of specific data and the name
associated with the variable is used to work around this reserved block.

The name given to a variable is known as its variable name. Usually a single variable
stores only the data belonging to a certain data type.

The name is so given to them because when the program executes there is subject to
change hence it varies from time to time.

R Programming Language is a dynamically typed language, i.e. the R Language


Variables are not declared with a data type rather they take the data type of the R-
object assigned to them.

This feature is also shown in languages like Python and PHP.


Creating Variables in R Language
Let’s look at ways of declaring and initializing variables in R language:

R supports three ways of variable assignment:

Using equal operator- operators use an arrow or an equal sign to assign values to
variables.
Using the leftward operator- data is copied from right to left.
Using the rightward operator- data is copied from left to right.

Syntax for creating R Variables

Types of Variable Creation in R:

Using equal to operators variable_name = value


using leftward operator variable_name <- value
using rightward operator value -> variable_name

Creating Variables in R With Example

Let’s look at the live example of creating Variables in R:


Nomenclature of R Variables
The following rules need to be kept in mind while naming a R variable:

A valid variable name consists of a combination of alphabets, numbers, dot(.), and


underscore(_) characters. Example: var.1_ is valid
Apart from the dot and underscore operators, no other special character is allowed.
Example: var$1 or var#1 both are invalid
Variables can start with alphabets or dot characters. Example: .var or var is valid
The variable should not start with numbers or underscore. Example: 2var or _var is
invalid.
If a variable starts with a dot the next thing after the dot cannot be a number.
Example: .3var is invalid
The variable name should not be a reserved keyword in R. Example: TRUE,
FALSE,etc.

Important Methods for R Variables


R provides some useful methods to perform operations on variables. These methods
are used to determine the data type of the variable, finding a variable, deleting a
variable, etc. Following are some of the methods used to work on variables:

1. class() function
This built-in function is used to determine the data type of the variable provided to it.
The R variable to be checked is passed to this as an argument and it prints the data
type in return.

Syntax

class(variable)

Example

var1 = "hello"

print(class(var1))

Output

[1] "character"

2. ls() function

This built-in function is used to know all the present variables in the workspace. This is
generally helpful when dealing with a large number of variables at once and helps
prevents overwriting any of them.

Syntax

ls()

Example

var1 = "hello"

var2 <- "hello"

"hello" -> var3

print(ls())

Output

[1] "var1" "var2" "var3"


3. rm() function

This is again a built-in function used to delete an unwanted variable within your
workspace. This helps clear the memory space allocated to certain variables that are
not in use thereby creating more space for others. The name of the variable to be
deleted is passed as an argument to it.

Syntax

rm(variable)

Example

# using equal to operator

var1 = "hello"

# using leftward operator

var2 <- "hello"

# using rightward operator

"hello" -> var3

# Removing variable

rm(var3)

print(var3)

Output

Error in print(var3) : object 'var3' not foundExecution halted

Scope of Variables in R programming


The location where we can find a variable and also access it if required is called
the scope of a variable. There are mainly two types of variable scopes:

1. Global Variables

Global variables are those variables that exist throughout the execution of a program. It
can be changed and accessed from any part of the program.
As the name suggests, Global Variables can be accessed from any part of the program.

They are available throughout the lifetime of a program.


They are declared anywhere in the program outside all of the functions or blocks.

Declaring global variables

Global variables are usually declared outside of all of the functions and blocks. They
can be accessed from any portion of the program.

# global variable

global = 5

# global variable accessed from

# within a function

display = function(){

print(global)

display()

# changing value of global variable

global = 10

display()

Output

[1] 5

[1] 10

In the above code, the variable ‘global’ is declared at the top of the program outside all
of the functions so it is a global variable and can be accessed or updated from
anywhere in the program.

2. Local Variables
Local variables are those variables that exist only within a certain part of a program like
a function and are released when the function call ends. Local variables do not exist
outside the block in which they are declared, i.e. they can not be accessed or used
outside that block.

Declaring local variables

Local variables are declared inside a block.

func = function(){

# this variable is local to the

# function func() and cannot be

# accessed outside this function

age = 18

print(age)

cat("Age is:\n")

func()

Output

Age is:

[1] 18

Difference between local and global variables in R


1. Scope A global variable is defined outside of any function and may be accessed
from anywhere in the program, as opposed to a local variable.
2. Lifetime A local variable’s lifetime is constrained by the function in which it is
defined. The local variable is destroyed once the function has finished running. A
global variable, on the other hand, doesn’t leave memory until the program is
finished running or the variable is explicitly deleted.
3. Naming conflicts If the same variable name is used in different portions of the
program, they may occur since a global variable can be accessed from anywhere in
the program. Contrarily, local variables are solely applicable to the function in which
they are defined, reducing the likelihood of naming conflicts.
4. Memory usage Because global variables are kept in memory throughout program
execution, they can eat up more memory than local variables. Local variables, on
the other hand, are created and destroyed only when necessary, therefore they
normally use less memory.

Types of R Variables
Depending on the type of data that you want to store, variables can be divided into the
following types.

1. Boolean Variables

It stores single bit data which is either TRUE or FALSE. Here, TRUE means yes

and FALSE means no. For example,

a = TRUE

print(a)

print(class(a))

Output

[1] TRUE

[1] "logical"

Here, we have declared the boolean variable a with the value TRUE. Boolean variables

belong to the logical class so class(a) returns "logical".

2. Integer Variables

It stores numeric data without any decimal values. For example,

A = 14L

print(A)

print(class(A))
Output

[1] 14

[1] "integer"

Here, L represents integer value. In R, integer variables belong to the integer class

so, class(a) returns "integer".

3. Floating Point Variables

It stores numeric data with decimal values. For example,

x = 13.4

print(x)

print(class(x))

Output

[1] 13.4

[1] "numeric"

Here, we have created a floating point variable named x. You can see that the floating

point variable belongs to the numeric class.

4. Character Variables

It stores a single character data. For example,

alphabet = "a"

print(alphabet)

print(class(alphabet))
Output

[1] "a"

[1] "character"

Here, we have created a character variable named alphabet. Since character variables

belong to the character class, class(alphabet) returns "character".

5. String Variables

It stores data that is composed of more than one character. We use double quotes to
represent string data. For example,

message = "Welcome to Programiz!"

print(message)

print(class(message))

Output

[1] "Welcome to Programiz!"

[1] "character"

Here, we have created a string variable named message. You can see that the string
variable also belongs to the character class.

Changing Value of Variables

Depending on the conditions or information passed into the program, you can change
the value of a variable. For example,

message = "Hello World!"

print(message)
# changing value of a variable

message <- "Welcome to Programiz!"

print(message)

Output

[1] "Hello World!"

[1] "Welcome to Programiz!"

In this program,

"Hello World!" - initial value of message

"Welcome to Programiz!" - changed value of message

You can see that the value of a variable can be changed anytime.

R Constants

Constants are those entities whose values aren't meant to be changed anywhere
throughout the code. In R, we can declare constants using the <- symbol. For example,

x <- "Welcome to Programiz!"

print(x)

Output

[1] "Welcome to Programiz!"

Here, "Welcome to Programiz!" is a string constant.

Note: Constants are also known as scalars.

Types of R Constants
In R, we have the following types of constants.

The five types of R constants - numeric, integer, complex, logical, string.

In addition to these, there are 4 specific types of R constants - Null, NA, Inf, NaN.

Let's discuss each of these types one by one.

1. Integer Constants

Integer constants are the integer values we use in our code. These constants end with the

letter L. For example,

x <- 15L

print(typeof(x))

print(class(x))

Output

[1] "integer"

[1] "integer"

Here, 15L is a constant which has been assigned to x. You can see that the type and class

of the constant is integer.

We can use different types of integer constants in our code. For example,

# hexadecimal value

x <- 0x15L

print(x)

# exponential value

x <- 1e5L

print(x)

Output
[1] 21

[1] 100000

2. Numeric Constants

In R programming, numeric constants can be integers (4), floating-point numbers (0.55), or

exponential numbers (3e-3). For example,

z <- 3e-3

print(z) # 0.003

print(class(z)) # "numeric"

y <- 3.4

print(y) # 3.4

print(class(z)) # "numeric"

Output

[1] 0.003

[1] "numeric"

[1] 3.4

[1] "numeric"

3. Logical Constants

Logical constants in R are either TRUE or FALSE. For example,

x <- TRUE

y <- FALSE

print(x)

print(y)

Output
[1] TRUE

[1] FALSE

Note: We can also use a single character to create logical constants. For example,

x <- T

print(x) # TRUE

4. String Constants
String constants are the string data we use in our code. For example,

message <- "Welcome to Programiz!!"

print(message)

Output

[1] "Welcome to Programiz!!"

5. Complex Constants

A complex constant is data that contains a real and an imaginary part (denoted by the

suffix i). For example,

y <- 3.2e-1i

print(y)

print(typeof(y))

Output

[1] 0+0.32i

[1] "complex"

Note: Complex constants can only be purely imaginary. For example,

y <- 3i

print(y) # 0+3i
print(typeof(y)) # "complex"

Special R Constants
R programming also provides 4 special types of constants.

NULL - to declare an empty R object. For example,


x <- NULL
print(x) # NULL

print(typeof(x)) # "NULL"

Inf/-Inf - represents positive and negative infinity. For example,


# result is too big so it represents positive infinity

a <- 2^2020

print(a) # Inf

# result is too big

# represents negative infinity

b <- -2^2020

print(b) # -Inf

NaN (Not a Number) - represents undefined numerical value. For example,


print(0/0) # NaN

print(Inf/Inf) # NaN

NA - represents value which is not available. For example,

print(NA + 20) # NA

Built-In R Constants
R programming provides some predefined constants that can be directly used in our
program. For example,

# print list of uppercase letters

print(LETTERS)
# print list of lowercase letters

print(letters)

# print 3 letters abbreviation of English months

print([Link])

# print numerical value of constant pi

print(pi)

Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"

[20] "T" "U" "V" "W" "X" "Y" "Z"

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"

[20] "t" "u" "v" "w" "x" "y" "z"

[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

[1] 3.141593

In the above example, we have used the following built-in constants:

LETTERS - to display a list of all uppercase letters

letters - to display a list of all small letters

[Link] - to print 3 letter abbreviations of all English months

pi - to print the numerical value of the constant pi

R Operators
Operators are the symbols directing the compiler to perform various kinds of operations
between the operands. Operators simulate the various mathematical, logical, and decision
operations performed on a set of Complex Numbers, Integers, and Numericals as input
operands.

R supports majorly four kinds of binary operators between a set of operands. In this
article, we will see various types of operators in R Programming language and their
usage.

Types of the operator in R language


Arithmetic Operators
Logical Operators
Relational Operators
Assignment Operators
Miscellaneous Operators

Arithmetic Operators
Arithmetic Operators modulo using the specified operator between operands, which may
be either scalar values, complex numbers, or vectors. The R operators are performed
element-wise at the corresponding positions of the vectors.

Addition operator (+)

The values at the corresponding positions of both operands are added. Consider the
following R operator snippet to add two vectors:

a <- c (1, 0.1)

b <- c (2.33, 4)

print (a+b)

Output : 3.33 4.10

Subtraction Operator (-)

The second operand values are subtracted from the first. Consider the following R
operator snippet to subtract two variables:

a <- 6

b <- 8.4

print (a-b)

Output : -2.4

Multiplication Operator (*)


The multiplication of corresponding elements of vectors and Integers are multiplied with
the use of the ‘*’ operator.

B= c(4,4)

C= c(5,5)

print (B*C)

Output : 20 20

Division Operator (/)

The first operand is divided by the second operand with the use of the ‘/’ operator.

a <- 10

b <- 5

print (a/b)

Output : 2

Power Operator (^)

The first operand is raised to the power of the second operand.

a <- 4

b <- 5

print(a^b)

Output : 1024

Modulo Operator (%%)

The remainder of the first operand divided by the second operand is returned.

list1<- c(2, 22)

list2<-c(2,4)

print(list1 %% list2)

Output : 0 2
The following R code illustrates the usage of all Arithmetic R operators.

# R program to illustrate the use of Arithmetic operators

vec1 <- c(0, 2)

vec2 <- c(2, 3)

# Performing operations on Operands

cat ("Addition of vectors :", vec1 + vec2, "\n")

cat ("Subtraction of vectors :", vec1 - vec2, "\n")

cat ("Multiplication of vectors :", vec1 * vec2, "\n")

cat ("Division of vectors :", vec1 / vec2, "\n")

cat ("Modulo of vectors :", vec1 %% vec2, "\n")

cat ("Power operator :", vec1 ^ vec2)

Output

Addition of vectors : 2 5

Subtraction of vectors : -2 -1

Multiplication of vectors : 0 6

Division of vectors : 0 0.6666667

Modulo of vectors : 0 2

Power operator : 0 8

Logical Operators
Logical Operators in R simulate element-wise decision operations, based on the specified
operator between the operands, which are then evaluated to either a True or False
boolean value. Any non-zero integer value is considered as a TRUE value, be it a complex
or real number.
Element-wise Logical AND operator (&)

Returns True if both the operands are True.

list1 <- c(TRUE, 0.1)

list2 <- c(0,4+3i)

print(list1 & list2)

Output : FALSE TRUE

Any non zero integer value is considered as a TRUE value, be it complex or real
number.

Element-wise Logical OR operator (|)

Returns True if either of the operands is True.

list1 <- c(TRUE, 0.1)

list2 <- c(0,4+3i)

print(list1|list2)

Output : TRUE TRUE

NOT operator (!)

A unary operator that negates the status of the elements of the operand.

list1 <- c(0,FALSE)

print(!list1)

Output : TRUE TRUE

Logical AND operator (&&)

Returns True if both the first elements of the operands are True.

list1 <- c(TRUE, 0.1)

list2 <- c(0,4+3i)


print(list1[1] && list2[1])

Output : FALSE

Compares just the first elements of both the lists.

Logical OR operator (||)

Returns True if either of the first elements of the operands is True.

list1 <- c(TRUE, 0.1)

list2 <- c(0,4+3i)

print(list1[1]||list2[1])

Output : TRUE

The following R code illustrates the usage of all Logical Operators in R:

# R program to illustrate the use of Logical operators

vec1 <- c(0,2)

vec2 <- c(TRUE,FALSE)

# Performing operations on Operands

cat ("Element wise AND :", vec1 & vec2, "\n")

cat ("Element wise OR :", vec1 | vec2, "\n")

cat ("Logical AND :", vec1[1] && vec2[1], "\n")

cat ("Logical OR :", vec1[1] || vec2[1], "\n")

cat ("Negation :", !vec1)

Output

Element wise AND : FALSE FALSE

Element wise OR : TRUE TRUE

Logical AND : FALSE

Logical OR : TRUE

Negation : TRUE FALSE


Relational Operators
The Relational Operators in R carry out comparison operations between the
corresponding elements of the operands. Returns a boolean TRUE value if the first
operand satisfies the relation compared to the second. A TRUE value is always
considered to be greater than the FALSE.

Less than (<)

Returns TRUE if the corresponding element of the first operand is less than that of the
second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1,"apple")

list2 <- c(0,0.1,"bat")

print(list1<list2)

Output : FALSE FALSE TRUE

Less than equal to (<=)

Returns TRUE if the corresponding element of the first operand is less than or equal to
that of the second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1, "apple")

list2 <- c(TRUE, 0.1, "bat")

# Convert lists to character strings

list1_char <- [Link](list1)

list2_char <- [Link](list2)

# Compare character strings

print(list1_char <= list2_char)

Output : TRUE TRUE TRUE


Greater than (>)

Returns TRUE if the corresponding element of the first operand is greater than that of the
second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1, "apple")

list2 <- c(TRUE, 0.1, "bat")

print(list1_char > list2_char)

Output : FALSE FALSE FALSE

Greater than equal to (>=)

Returns TRUE if the corresponding element of the first operand is greater or equal to that
of the second operand. Else returns FALSE.

list1 <- c(TRUE, 0.1, "apple")

list2 <- c(TRUE, 0.1, "bat")

print(list1_char >= list2_char)

Output : TRUE TRUE FALSE

Not equal to (!=)

Returns TRUE if the corresponding element of the first operand is not equal to the second
operand. Else returns FALSE.

list1 <- c(TRUE, 0.1,'apple')

list2 <- c(0,0.1,"bat")

print(list1!=list2)

Output : TRUE FALSE TRUE

The following R code illustrates the usage of all Relational Operators in R:

# R program to illustrate the use of Relational operators

vec1 <- c(0, 2)

vec2 <- c(2, 3)


# Performing operations on Operands

cat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")

cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")

cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")

cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")

cat ("Vector1 not equal to Vector2 :", vec1 != vec2, "\n")

Output

Vector1 less than Vector2 : TRUE TRUE

Vector1 less than equal to Vector2 : TRUE TRUE

Vector1 greater than Vector2 : FALSE FALSE

Vector1 greater than equal to Vector2 : FALSE FALSE

Vector1 not equal to Vector2 : TRUE TRUE

Assignment Operators
Assignment Operators in R are used to assigning values to various data objects in R. The
objects may be integers, vectors, or functions. These values are then stored by the
assigned variable names. There are two kinds of assignment operators: Left and Right

Left Assignment (<- or <<- or =)

Assigns a value to a vector.

vec1 = c("ab", TRUE)

print (vec1)

Output : "ab" "TRUE"

Right Assignment (-> or ->>)

Assigns value to a vector.


c("ab", TRUE) ->> vec1

print (vec1)

Output : "ab" "TRUE"

The following R code illustrates the usage of all Relational Operators in R:

# R program to illustrate the use of Assignment operators

vec1 <- c(2:5)

c(2:5) ->> vec2

vec3 <<- c(2:5)

vec4 = c(2:5)

c(2:5) -> vec5

# Performing operations on Operands

cat ("vector 1 :", vec1, "\n")

cat("vector 2 :", vec2, "\n")

cat ("vector 3 :", vec3, "\n")

cat("vector 4 :", vec4, "\n")

cat("vector 5 :", vec5)

Output

vector 1 : 2 3 4 5

vector 2 : 2 3 4 5

vector 3 : 2 3 4 5

vector 4 : 2 3 4 5

vector 5 : 2 3 4 5

Miscellaneous Operators
Miscellaneous Operator are the mixed operators in R that simulate the printing of
sequences and assignment of vectors, either left or right-handed.

%in% Operator

Checks if an element belongs to a list and returns a boolean value TRUE if the value is
present else FALSE.

val <- 0.1

list1 <- c(TRUE, 0.1,"apple")

print (val %in% list1)

Output : TRUE

Checks for the value 0.1 in the specified list. It exists, therefore, prints
TRUE.

%*% Operator

This operator is used to multiply a matrix with its transpose. Transpose of the matrix is
obtained by interchanging the rows to columns and columns to rows. The number of
columns of the first matrix must be equal to the number of rows of the second matrix.
Multiplication of the matrix A with its transpose, B, produces a square
matrix. Ar∗cxBc∗r−>Pr∗r Ar∗cxBc∗r−>Pr∗r
​ ​ ​

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

print (mat)

print( t(mat))

pro = mat %*% t(mat)

print(pro)

Input :

Output :[,1] [,2] [,3] #original matrix of order 2x3

[1,] 1 3 5

[2,] 2 4 6

[,1] [,2] #transposed matrix of order 3x2

[1,] 1 2
[2,] 3 4

[3,] 5 6

[,1] [,2] #product matrix of order 2x2

[1,] 35 44

[2,] 44 56

The following R code illustrates the usage of all Miscellaneous Operators in R:

# R program to illustrate the use of Miscellaneous operators

mat <- matrix (1:4, nrow = 1, ncol = 4)

print("Matrix elements using : ")

print(mat)

product = mat %*% t(mat)

print("Product of matrices")

print(product,)

cat ("does 1 exist in prod matrix :", "1" %in% product)

Output

[1] "Matrix elements using : "

[,1] [,2] [,3] [,4]

[1,] 1 2 3 4

[1] "Product of matrices"

[,1]

[1,] 30

does 1 exist in prod matrix : FALSE


R Data Types
Different forms of data that can be saved and manipulated are defined and categorized
using data types in computer languages, including R. Each R data type has unique
properties and associated operations.

What are R Data types?


R Data types are used to specify the kind of data that can be stored in a variable. For
effective memory consumption and precise computation, the right data type must be
selected. Each R data type has its own set of regulations and restrictions. Variables are
not needed to be declare with a data type in R, data type even can be changed.

Example of R data Type:

#numeric data type

var <- 30

#integer data type

var <- 80L

Data Types in R Programming Language


Each variable in R has an associated data type. Each R-Data Type requires different
amounts of memory and has some specific operations which can be performed over it.

Data Types in R are:

1. numeric – (3,6.7,121)
2. Integer – (2L, 42L; where ‘L’ declares this as an integer)
3. logical – (‘True’)
4. complex – (7 + 5i; where ‘i’ is imaginary number)
5. character – (“a”, “B”, “c is third”, “69”)
6. raw – ( [Link](55); raw creates a raw vector of the specified length)

R Programming language has the following basic R-data types and the following table
shows the data type and the values that each data type can take.
Basic Data Types Values Examples

Numeric Set of all real numbers "numeric_value <- 3.14"

Integer Set of all integers, Z "integer_value <- 42L"

Logical TRUE and FALSE "logical_value <- TRUE"

Complex Set of complex numbers "complex_value <- 1 + 2i"

“a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, "character_value <- "Hello
Character
“2”, …etc Geeks"

raw [Link]() "single_raw <- [Link](255)"

1. Numeric Data type in R

Decimal values are called numeric in R. It is the default R data type for numbers in R. If
you assign a decimal value to a variable x as follows, x will be of numeric type. Real
numbers with a decimal point are represented using this data type in R. It uses a format
for double-precision floating-point numbers to represent numerical values.

# Assign a decimal value to x

x = 5.6

# print the class name of variable

print(class(x))

# print the type of variable

print(typeof(x))

Output

[1] "numeric"
[1] "double"

Even if an integer is assigned to a variable y, it is still saved as a numeric value.

# Assign an integer value to y

y=5

# print the class name of variable

print(class(y))

# print the type of variable

print(typeof(y))

Output

[1] "numeric"

[1] "double"

When R stores a number in a variable, it converts the number into a “double” value or a
decimal type with at least two decimal places.

This means that a value such as “5” here, is stored as 5.00 with a type of double and a
class of numeric. And also y is not an integer here can be confirmed with
the [Link]() function.

# Assign a integer value to y

y=5

# is y an integer?

print([Link](y))

Output

[1] FALSE

2. Integer Data type in R


R supports integer data types which are the set of all integers.

You can create as well as convert a value into an integer type using
the [Link]() function.

You can also use the capital ‘L’ notation as a suffix to denote that a particular value is of
the integer R data type.

# Create an integer value

x = [Link](5)

# print the class name of x

print(class(x))

# print the type of x

print(typeof(x))

# Declare an integer by appending an L suffix.

y = 5L

# print the class name of y

print(class(y))

# print the type of y

print(typeof(y))

Output

[1] "integer"

[1] "integer"

[1] "integer"

[1] "integer"

3. Logical Data type in R

R has logical data types that take either a value of true or false.

A logical value is often created via a comparison between variables.


Boolean values, which have two possible values, are represented by this R data type:
FALSE or TRUE

# Sample values

x=4

y=3

# Comparing two values

z=x>y

# print the logical value

print(z)

# print the class name of z

print(class(z))

# print the type of z

print(typeof(z))

Output

[1] TRUE

[1] "logical"

[1] "logical"

4. Complex Data type in R

R supports complex data types that are set of all the complex numbers. The complex data
type is to store numbers with an imaginary component.
# Assign a complex value to x

x = 4 + 3i

# print the class name of x

print(class(x))

# print the type of x

print(typeof(x))

Output

[1] "complex"

[1] "complex"

5. Character Data type in R

R supports character data types where you have all the alphabets and special characters.
It stores character values or strings. Strings in R can contain alphabets, numbers, and
symbols.

The easiest way to denote that a value is of character type in R data type is to wrap the
value inside single or double inverted commas.

# Assign a character value to char

char = "Geeksforgeeks"

# print the class name of char

print(class(char))

# print the type of char

print(typeof(char))

Output

[1] "character"

[1] "character"
There are several tasks that can be done using R data types. Let’s understand each task
with its action and the syntax for doing the task along with an R code to illustrate the
task.

6. Raw data type in R

To save and work with data at the byte level in R, use the raw data type. By displaying a
series of unprocessed bytes, it enables low-level operations on binary data. Here are
some speculative data on R’s raw data types:

# Create a raw vector

x <- [Link](c(0x1, 0x2, 0x3, 0x4, 0x5))

print(x)

Output

[1] 01 02 03 04 05

Five elements make up this raw vector x, each of which represents a raw byte value.

Find Data Type of an Object in R


To find the data type of an object you have to use class() function. The syntax for doing
that is you need to pass the object as an argument to the function class() to find the data
type of an object.

Syntax
class(object)

Example
# Logical

print(class(TRUE))

# Integer

print(class(3L))

# Numeric

print(class(10.5))

# Complex

print(class(1+2i))

# Character

print(class("12-04-2020"))

Output

[1] "logical"

[1] "integer"

[1] "numeric"

[1] "complex"

[1] "character"

Type verification
You can verify the data type of an object, if you doubt about it’s data type. To do that, you
need to use the prefix “is.” before the data type as a command.

Syntax:

is.data_type(object)

Example
# Logical

print([Link](TRUE))

# Integer

print([Link](3L))

# Numeric

print([Link](10.5))

# Complex

print([Link](1+2i))

# Character

print([Link]("12-04-2020"))

print([Link]("a"))

print([Link](2+3i))

Output

[1] TRUE

[1] TRUE

[1] TRUE

[1] TRUE

[1] TRUE

[1] FALSE

[1] FALSE

Coerce or Convert the Data Type of an Object to Another


The process of altering the data type of an object to another type is referred to as coercion
or data type conversion. This is a common operation in many programming languages
that is used to alter data and perform various computations.
When coercion is required, the language normally performs it automatically, whereas
conversion is performed directly by the programmer.

Coercion can manifest itself in a variety of ways, depending on the R programming


language and the context in which it is employed.

In some circumstances, the coercion is implicit, which means that the language will
change one type to another without the programmer having to expressly request it.

Syntax

as.data_type(object)

Note: All the coercions are not possible and if attempted will be returning an “NA” value.

For Detailed Explanation – Data Type Conversion in R

Example

# Logical

print([Link](TRUE))

# Integer

print([Link](3L))

# Numeric

print([Link](10.5))

# Complex

print([Link](1+2i))

# Can't possible

print([Link]("12-04-2020"))

Output

[1] 1[1] 3+0i[1] TRUE[1] "1+2i"[1] NAWarning message:In print([Link]("12-04-


2020")) : NAs introduced by coercion

R – Objects
Every programming language has its own data types to store values or any information
so that the user can assign these data types to the variables and perform operations
respectively. Operations are performed accordingly to the data [Link] data types
can be character, integer, float, long, etc. Based on the data type, memory/storage is
allocated to the variable. For example, in C language character variables are assigned
with 1 byte of memory, integer variable with 2 or 4 bytes of memory and other data
types have different memory allocation for them. Unlike other programming languages,
variables are assigned to objects rather than data types in R programming.

Type of Objects

There are 5 basic types of objects in the R language:

Vectors

Atomic vectors are one of the basic types of objects in R programming. Atomic vectors
can store homogeneous data types such as character, doubles, integers, raw, logical,
and complex. A single element variable is also said to be vector.

Example:

# Create vectors

x <- c(1, 2, 3, 4)

y <- c("a", "b", "c", "d")

z <- 5

# Print vector and class of vector

print(x)

print(class(x))

print(y)

print(class(y))

print(z)

print(class(z))

Output:
[1] 1 2 3 4

[1] "numeric"

[1] "a" "b" "c" "d"

[1] "character"

[1] 5

[1] "numeric"

Lists

List is another type of object in R programming. List can contain heterogeneous data
types such as vectors or another lists.

Example:

# Create list

ls <- list(c(1, 2, 3, 4), list("a", "b", "c"))

# Print

print(ls)

print(class(ls))

Output:

[[1]]

[1] 1 2 3 4

[[2]]

[[2]][[1]]

[1] "a"

[[2]][[2]]

[1] "b"
[[2]][[3]]

[1] "c"

[1] "list"

Matrices

To store values as 2-Dimensional array, matrices are used in R. Data, number of rows
and columns are defined in the matrix() function.

Syntax:

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)

Example:

x <- c(1, 2, 3, 4, 5, 6)

# Create matrix

mat <- matrix(x, nrow = 2)

print(mat)

print(class(mat))

Output:

[, 1] [, 2] [, 3]

[1, ] 1 3 5

[2, ] 2 4 6

[1] "matrix"

Factors
Factor object encodes a vector of unique elements (levels) from the given data vector.

Example:

# Create vector

s <- c("spring", "autumn", "winter", "summer",

"spring", "autumn")

print(factor(s))

print(nlevels(factor(s)))

Output:

[1] spring autumn winter summer spring autumn

Levels: autumn spring summer winter

[1] 4

Arrays

array()function is used to create n-dimensional array. This function takes dim attribute
as an argument and creates required length of each dimension as specified in the
[Link]:

array(data, dim = length(data), dimnames = NULL)

Example:

# Create 3-dimensional array

# and filling values by column

arr <- array(c(1, 2, 3), dim = c(3, 3, 3))

print(arr)

Output:

,, 1
[, 1] [, 2] [, 3]

[1, ] 1 1 1

[2, ] 2 2 2

[3, ] 3 3 3,, 2

[, 1] [, 2] [, 3]

[1, ] 1 1 1

[2, ] 2 2 2

[3, ] 3 3 3,, 3

[, 1] [, 2] [, 3]

[1, ] 1 1 1

[2, ] 2 2 2

[3, ] 3 3 3

Data Frames

Data frames are 2-dimensional tabular data object in R programming. Data frames
consists of multiple columns and each column represents a vector. Columns in data
frame can have different modes of data unlike matrices.

Example:
# Create vectors

x <- 1:5

y <- LETTERS[1:5]

z <- c("Albert", "Bob", "Charlie", "Denver", "Elie")

# Create data frame of vectors

df <- [Link](x, y, z)

# Print data frame

print(df)

Output:

x y z

1 1 A Albert

2 2 B Bob

3 3 C Charlie

4 4 D Denver

5 5 E Elie

Taking Input from User in R Programming


Developers often have a need to interact with users, either to get data or to provide some
sort of result. Most programs today use a dialog box as a way of asking the user to
provide some type of input. Like other programming languages in R it’s also possible to
take input from the user. For doing so, there are two methods in R.

Using readline() method


Using scan() method

Using readline() method

In R language readline() method takes input in string format. If one inputs an integer
then it is inputted as a string, lets say, one wants to input 255, then it will input
as “255”, like a string. So one needs to convert that inputted value to the format that he
needs. In this case, string “255” is converted to integer 255. To convert the inputted
value to the desired data type, there are some functions in R,

[Link](n); —> convert to integer


[Link](n); —> convert to numeric type (float, double etc)
[Link](n); —> convert to complex number (i.e 3+2i)
[Link](n) —> convert to date …, etc

Syntax: var = readline(); var = [Link](var);Note that one can use “<-“ instead of “=”

Example:

# taking input using readline()

var = readline();

# convert the inputted value to integer

var = [Link](var);

# print the value

print(var)

Output:

255

[1] 255

Taking multiple inputs in R

Taking multiple inputs in R language is same as taking single input, just need to define
multiple readline() for inputs. One can use braces for define multiple readline() inside
it.

Syntax: var1 = readline(“Enter 1st number : “); var2 = readline(“Enter 2nd number :
“); var3 = readline(“Enter 3rd number : “); var4 = readline(“Enter 4th number : “);or,{ var1
= readline(“Enter 1st number : “); var2 = readline(“Enter 2nd number : “); var3 =
readline(“Enter 3rd number : “); var4 = readline(“Enter 4th number : “); }
Example:

# taking multiple inputs

# using braces

var1 = readline("Enter 1st number : ");

var2 = readline("Enter 2nd number : ");

var3 = readline("Enter 3rd number : ");

var4 = readline("Enter 4th number : ");

# converting each value

var1 = [Link](var1);

var2 = [Link](var2);

var3 = [Link](var3);

var4 = [Link](var4);

# print the sum of the 4 number

print(var1 + var2 + var3 + var4)

Output:

Enter 1st number : 12

Enter 2nd number : 13

Enter 3rd number : 14

Enter 4th number : 15

[1] 54

Taking String and Character input in R

To take string input is the same as an integer. For “String” one doesn’t need to convert
the inputted data into a string because R takes input as string always. And for
“character”, it needs to be converted to ‘character’. Sometimes it may not cause any
error. One can take character input as same as string also, but that inputted data is of
type string for the entire program. So the best way to use that inputted data as
‘character’ is to convert the data to a character.

Syntax:string: var1 = readline(prompt = “Enter your name : “);character: var1 =


readline(prompt = “Enter any character : “); var1 = [Link](var1)

Example:

# string input

var1 = readline(prompt = "Enter your name : ");

# character input

var2 = readline(prompt = "Enter any character : ");

# convert to character

var2 = [Link](var2)

# printing values

print(var1)

print(var2)

Output:

Enter your name : GeeksforGeeks

Enter any character : G

[1] "GeeksforGeeks"

[1] "G"

Using scan() method

Another way to take user input in R language is using a method, called scan() method.
This method takes input from the console. This method is a very handy method while
inputs are needed to taken quickly for any mathematical calculation or for any dataset.
This method reads data in the form of a vector or list.

This method also uses to reads input from a file also.

Syntax: x = scan()

scan() method is taking input continuously, to terminate the input process, need to
press Enter key 2 times on the console.

Example: This is simple method to take input using scan() method, where some integer
number is taking as input and print those values in the next line on the console.

# R program to illustrate taking input from the user

# taking input using scan()

x = scan()

# print the inputted values

print(x)

Output:

1: 1 2 3 4 5 6

7: 7 8 9 4 5 6

13:

Read 12 items

[1] 1 2 3 4 5 6 7 8 9 4 5 6

Explanation: Total 12 integers are taking as input in 2 lines when the control goes to
3rd line then by pressing Enter key 2 times the input process will be terminated.

1. Taking double, string, character type values using scan() method

To take double, string, character types inputs, specify the type of the inputted value in
the scan() method. To do this there is an argument called what, by which one can
specify the data type of the inputted value.

Syntax: x = scan(what = double()) —-for double x = scan(what = ” “) —-for string x =


scan(what = character()) —-for character
Example:

# R program to illustrate taking input from the user

# double input using scan()

d = scan(what = double())

# string input using 'scan()'

s = scan(what = " ")

# character input using 'scan()'

c = scan(what = character())

# print the inputted values

print(d) # double

print(s) # string

print(c) # character

Output:

1: 123.321 523.458 632.147

4: 741.25 855.36

6:

Read 5 items

1: geeksfor geeks gfg

4: c++ R java python

8:

Read 7 items

1: g e e k s f o

8: r g e e k s

14:
Read 13 items

[1] 123.321 523.458 632.147 741.250 855.360

[1] "geeksfor" "geeks" "gfg" "c++" "R" "java" "python"

[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

Explanation: Here, count of double items is 5, count of sorting items is 7, count of


character items is 13.

1. Read File data using scan() method

To read file using scan() method is same as normal console input, only thing is that, one
needs to pass the file name and data type to the scan() method.

Syntax: x = scan(“[Link]”, what = double()) —-for double x =


scan(“[Link]”, what = ” “) —-for string x = scan(“[Link]”, what = character())
—-for character

Example:

# R program to illustrate taking input from the user

# string file input using scan()

s = scan("[Link]", what = " ")

# double file input using scan()

d = scan("[Link]", what = double())

# character file input using scan()

c = scan("[Link]", what = character())

# print the inputted values

print(s) # string

print(d) # double

print(c) # character

Output:

Read 7 items
Read 5 items

Read 13 items

[1] "geek" "for" "geeks" "gfg" "c++" "java" "python"

[1] 123.321 523.458 632.147 741.250 855.360

[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

Save the data file in the same location where the program is saved for better access.
Otherwise total path of the file need to defined inside the scan() method.

R Built-in Functions
The functions which are already created or defined in the programming framework are
known as a built-in function. R has a rich set of functions that can be used to perform almost
every task for the user. These built-in functions are divided into the following categories based
on their functionality.

Math Functions
R provides the various mathematical functions to perform the mathematical calculation.
These mathematical functions are very helpful to find absolute value, square value and much
more calculations. In R, there are the following functions which are used:
S. No Function Description Example

x<- -4

1. abs(x) It returns the absolute value of input print(abs(x))


x.
Output
[1] 4

x<- 4

print(sqrt(x))
2. sqrt(x) It returns the square root of input x.
Output

[1] 2

x<- 4.5

It returns the smallest integer which print(ceiling(x))


3. ceiling(x)
is larger than or equal to x. Output

[1] 5

x<- 2.5

It returns the largest integer, which print(floor(x))


4. floor(x)
is smaller than or equal to x. Output

[1] 2

x<- c(1.2,2.5,8.1)

print(trunc(x))
5. trunc(x) It returns the truncate value of input
x.
Output
[1] 1 2 8

x<- -4

print(abs(x))
6. round(x, digits=n) It returns round value of input x.
Output
4

x<- 4

print(cos(x))
print(sin(x))

7. cos(x), sin(x), tan(x) It returns cos(x), sin(x) value of input print(tan(x))


x.
Output
[1] -06536436

[2] -0.7568025

[3] 1.157821

x<- 4

It returns natural logarithm of input print(log(x))


8. log(x) x. Output

[1] 1.386294

x<- 4

print(log10(x))
It returns common logarithm of
9. log10(x) input x.
Output

[1] 0.60206

x<- 4

print(exp(x))
10. exp(x) It returns exponent.
Output

[1] 54.59815

String Function
R provides various string functions to perform tasks. These string functions allow us to extract
sub string from string, search pattern etc. There are the following string functions in R:
S. No Function Description Example

a <- "987654321"

It is used to extract substr(a, 3, 3)


1. substr(x, substrings in a character
start=n1,stop=n2)
vector. Output
[1] "3"

st1 <- c('abcd','bdcd','abcdabcd')

pattern<- '^abc'
grep(pattern, x ,
2. [Link]=FALSE, It searches for pattern in x. print(grep(pattern, st1))
fixed=FALSE)
Output
[1] 1 3

st1<- "England is beautiful but no


the part of EU"
sub(pattern,
replacement, x, It finds pattern in x and sub("England', "UK", st1)
3. replaces it with replacement
[Link] =FALSE, (new) text. Output
fixed=FALSE)
[1] "UK is beautiful but not a part
of EU"

paste('one',2,'three',4,'five')
It concatenates strings after
4. paste(..., sep="") using sep string to separate Output
them.
[1] one 2 three 4 five

a<-"Split all the character"

print(strsplit(a, ""))
It splits the elements of
5. strsplit(x, split) character vector x at split Output
point.
[[1]]

[1] "split" "all" "the" "character"

st1<- "shuBHAm"

print(tolower(st1))
6. tolower(x) It is used to convert the
string into lower case.
Output
[1] shubham
st1<- "shuBHAm"

print(toupper(st1))
7. toupper(x) It is used to convert the
string into upper case. Output
[1] SHUBHAM

Statistical Probability Functions


R provides various statistical probability functions to perform statistical task. These statistical
functions are very helpful to find normal density, normal quantile and many more calculation.
In R, there are following functions which are used:
S. No Function Description Example

a <- seq(-7, 7, by=0.1)

b <- dnorm(a, mean=2.5,


sd=0.5)
It is used to find the height of
1. dnorm(x, m=0, sd=1, the probability distribution at png(file="[Link]")
log=False) each point to a given mean
and standard deviation plot(x,y)

[Link]()

a <- seq(-7, 7, by=0.2)

b <- dnorm(a, mean=2.5,


sd=2)
it is used to find the probability
pnorm(q, m=0, sd=1, of a normally distributed png(file="[Link]")
2. [Link]=TRUE, random numbers which are
log.p=FALSE) less than the value of a given
plot(x,y)
number.
[Link]()

a <- seq(1, 2, by=002)

b <- qnorm(a, mean=2.5,


sd=0.5)
It is used to find a number
whose cumulative value png(file="[Link]")
3. qnorm(p, m=0, sd=1)
matches with the probability
value. plot(x,y)

[Link]()

y <- rnorm(40)

png(file="[Link]")
It is used to generate random
4. rnorm(n, m=0, sd=1) numbers whose distribution is hist(y, main="Normal
Distribution")
normal.
[Link]()

a<-seq(0, 40, by=1)

b<- dbinom(a, 40, 0.5)


5. dbinom(x, size, prob) It is used to find the probability png(file="[Link]")
density distribution at each
point. plot(x,y)

[Link]()

a <- pbinom(25, 40,0.5)


It is used to find the print(a)
6. pbinom(q, size, prob) cumulative probability (a single
value representing the
probability) of an event. Output
[1] 0.9596548

a <- qbinom(0.25, 40,01/2)

It is used to find a number print(a)


7. qbinom(p, size, prob) whose cumulative value
matches the probability value. Output
[1] 18

a <- rbinom(6, 140,0.4)


It is used to generate required print(a)
number of random values of a
8. rbinom(n, size, prob)
given probability from a given Output
sample.
[1] 55 61 46 56 58 49

dpois(a=2,
lambda=3)+dpois(a=3,
it is the probability of x lambda=3)+dpois(z=4,
successes in a period when the labda=4)
9. dpois(x, lamba) expected number of events is
lambda (λ) Output
[1] 0.616115

ppois(q=4, lambda=3,
[Link]=TRUE)-ppois(q=1,
It is a cumulative probability of lambda=3, [Link]=TRUE)
10. ppois(q, lamba) less than or equal to q
successes. Output
[1] 0.6434504

It is used to generate random rpois(10, 10)


11. rpois(n, lamba) numbers from the poisson
distribution. [1] 6 10 11 3 10 7 7 8 14 12

This function provide


information about the uniform
12. dunif(x, min=0, distribution on the interval dunif(x, min=0, max=1,
max=1) from min to max. It gives the log=FALSE)
density.

punif(q, min=0, max=1,


punif(q, min=0,
13. max=1) It gives the distributed function [Link]=TRUE,
log.p=FALSE)

qunif(p, min=0, max=1,


14. qunif(p, min=0, It gives the quantile function. [Link]=TRUE,
max=1) log.p=FALSE)

15. runif(x, min=0, max=1) It generates random deviates. runif(x, min=0, max=1)

Other Statistical Function


Apart from the functions mentioned above, there are some other useful functions which
helps for statistical purpose. There are the following functions:
S. No Function Description Example

a<-c(0:10, 40)

xm<-mean(a)
mean(x, trim=0, It is used to find the mean for print(xm)
1. [Link]=FALSE) x object
Output
[1] 7.916667

a<-c(0:10, 40)

xm<-sd(a)
It returns standard deviation
2. sd(x) of an object. print(xm)

Output

[1] 10.58694

a<-c(0:10, 40)

xm<-meadian(a)

3. median(x) It returns median. print(xm)

Output
[1] 5.5

It returns quantile where x is


the numeric vector whose
4. quantilie(x, probs) quantiles are desired and
probs is a numeric vector with
probabilities in [0, 1]

a<-c(0:10, 40)

xm<-range(a)

5. range(x) It returns range. print(xm)

Output
[1] 0 40

a<-c(0:10, 40)

xm<-sum(a)

6. sum(x) It returns sum. print(xm)


Output
[1] 95
a<-c(0:10, 40)

xm<-diff(a)

7. diff(x, lag=1) It returns differences with lag print(xm)


indicating which lag to use.
Output

[1] 1 1 1 1 1 1 1 1 1 1 30

a<-c(0:10, 40)

xm<-min(a)

8. min(x) It returns minimum value. print(xm)

Output
[1] 0

a<-c(0:10, 40)

xm<-max(a)

9. max(x) It returns maximum value print(xm)

Output
[1] 40

a <- matrix(1:9,3,3)

scale(x)

Output
[,1]

[1,] -0.747776547

[2,] -0.653320562

[3,] -0.558864577

[4,] -0.464408592

[5,] -0.369952608

scale(x, Column center or standardize [6,] -0.275496623


10. center=TRUE,
scale=TRUE) a matrix. [7,] -0.181040638

[8,] -0.086584653

[9,] 0.007871332
[10,] 0.102327317

[11,] 0.196783302

[12,] 3.030462849

attr(,"scaled:center")

[1] 7.916667

attr(,"scaled:scale")

[1] 10.58694

R Vectors
R Vectors are the same as the arrays in R language which are used to hold multiple data
values of the same type. One major key point is that in R Programming Language the
indexing of the vector will start from ‘1’ and not from ‘0’. We can create numeric vectors
and character vectors as well.

Creating a vector
A vector is a basic data structure that represents a one-dimensional array. to create a
array we use the “c” function which the most common method use in R Programming
Language.

# R program to create Vectors we can use the c function to combine the values as a vector. By default the type will be
double

X<- c(61, 4, 21, 67, 89, 2)

cat('using c function', X, '\n')

# seq() function for creating a sequence of continuous values.

# [Link] defines the length of vector.

Y<- seq(1, 10, [Link] = 5)

cat('using seq() function', Y, '\n')

# use':' to create a vector of continuous values.

Z<- 2:7

cat('using colon', Z)

Output:

using c function 61 4 21 67 89 2 using seq() function 1 3.25 5.5 7.75 10 using


colon 2 3 4 5 6 7

Types of R vectors
Vectors are of different types which are used in R. Following are some of the types of
vectors:

Numeric vectors

Numeric vectors are those which contain numeric values such as integer, float, etc.
# R program to create numeric Vectors creation of vectors using c() function.

v1<- c(4, 5, 6, 7)

# display type of vector

typeof(v1)

# by using 'L' we can specify that we want integer values.

v2<- c(1L, 4L, 2L, 5L)

# display type of vector

typeof(v2)

Output:

[1] "double"[1] "integer"

Character vectors

Character vectors in R contain alphanumeric values and special characters.

# R program to create Character Vectors by default numeric values are converted into characters

v1<- c('geeks', '2', 'hello', 57)

# Displaying type of vector

typeof(v1)

Output:

[1] "character"

Logical vectors

Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for Null
values.
# R program to create Logical Vectors Creating logical vector using c() function

v1<- c(TRUE, FALSE, TRUE, NA)

# Displaying type of vector

typeof(v1)

Output:

[1] "logical"

Length of R vector
In R, the length of a vector is determined by the number of elements it contains. we can
use the length() function to retrieve the length of a vector.

# Create a numeric vector

x <- c(1, 2, 3, 4, 5)

# Find the length of the vector

length(x)

# Create a character vector

y <- c("apple", "banana", "cherry")

# Find the length of the vector

length(y)

# Create a logical vector

z <- c(TRUE, FALSE, TRUE, TRUE)

# Find the length of the vector

length(z)

Output:

> length(x)[1] 5> length(y)[1] 3> length(z)[1] 4


Accessing R vector elements
Accessing elements in a vector is the process of performing operation on an individual
element of a vector. There are many ways through which we can access the elements of
the vector. The most common is using the ‘[]’, symbol.

Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.

# R program to access elements of a Vector

# accessing elements with an index number.

X<- c(2, 5, 18, 1, 12)

cat('Using Subscript operator', X[2], '\n')

# by passing a range of values

# inside the vector index.

Y<- c(4, 8, 2, 1, 17)

cat('Using combine() function', Y[c(4, 1)], '\n')

Output:

Using Subscript operator 5 Using combine() function 1 4

Modifying a R vector
Modification of a Vector is the process of applying some operation on an individual
element of a vector to change its value in the vector. There are different ways through
which we can modify a vector:
# R program to modify elements of a Vector

# Creating a vector

X<- c(2, 7, 9, 7, 8, 2)

# modify a specific element

X[3] <- 1

X[2] <-9

cat('subscript operator', X, '\n')

# Modify using different logics.

X[1:5]<- 0

cat('Logical indexing', X, '\n')

# Modify by specifying the position or elements.

X<- X[c(3, 2, 1)]

cat('combine() function', X)

Output:

subscript operator 2 9 1 7 8 2 Logical indexing 0 0 0 0 0 2 combine() function 0


0 0

Deleting a R vector
Deletion of a Vector is the process of deleting all of the elements of the vector. This can be
done by assigning it to a NULL value.
# R program to delete a Vector

# Creating a Vector

M<- c(8, 10, 2, 5)

# set NULL to the vector

M<- NULL

cat('Output vector', M)

Output:

Output vector NULL

Sorting elements of a R Vector


sort() function is used with the help of which we can sort the values in ascending or
descending order.

# R program to sort elements of a Vector

# Creation of Vector

X<- c(8, 2, 7, 1, 11, 2)

# Sort in ascending order

A<- sort(X)

cat('ascending order', A, '\n')

# sort in descending order

# by setting decreasing as TRUE

B<- sort(X, decreasing = TRUE)

cat('descending order', B)

Output:
ascending order 1 2 2 7 8 11descending order 11 8 7 2 2 1

Vector arithmetic
Vector arithmetic in R programming refers to performing element-wise operations on
vectors. R is designed to handle vectorized operations efficiently, so you can perform
arithmetic without explicit loops. Here's a quick overview:

1. Basic Arithmetic Operations

Operations are performed element-wise if vectors have the same length.

# Example vectors

x <- c(1, 2, 3)

y <- c(4, 5, 6)

# Addition

x+y # Output: 5 7 9

# Subtraction

x - y # Output: -3 -3 -3

# Multiplication

x * y # Output: 4 10 18

# Division

x / y # Output: 0.25 0.4 0.5

# Modulus (remainder)

x %% y # Output: 1 2 3

# Exponentiation

x^2 # Output: 1 4 9

2. Operations with Scalars

If a scalar is used with a vector, it is recycled across all elements.


x <- c(1, 2, 3)

# Adding a scalar

x + 2 # Output: 3 4 5

# Multiplying by a scalar

x * 3 # Output: 3 6 9

3. Recycling Rule

When vectors of unequal lengths are used, the shorter one is recycled.

x <- c(1, 2, 3)

y <- c(4, 5)

# Recycling y

x + y # Output: 5 7 7 (y becomes c(4, 5, 4))

4. Logical Operations

Logical operations are also element-wise and return a logical vector.

x <- c(1, 2, 3)

y <- c(2, 2, 2)

# Comparison

x > y # Output: FALSE FALSE TRUE

x == y # Output: FALSE TRUE FALSE

5. Functions on Vectors

Common functions for vector arithmetic include:

# Sum

sum(x) # Output: 6 (1 + 2 + 3)
# Mean

mean(x) # Output: 2

# Cumulative sum

cumsum(x) # Output: 1 3 6

# Dot product

sum(x * y) # Output: 12

Notes:

R uses 1-based indexing, so ensure you handle vector indices accordingly.


Handle mismatched lengths carefully to avoid unexpected behaviour from recycling.

You might also like