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

Vectors

A vector in R is a fundamental data structure that contains a sequence of elements of the same data type, including logical, integer, double, character, complex, or raw types. Vectors can be accessed using indexing, and various functions like rep(), seq(), and is.vector() are available for manipulation and analysis. Key properties of vectors include their type, length, and attributes, with atomic vectors differing from lists in that all elements in a vector are of the same type.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Vectors

A vector in R is a fundamental data structure that contains a sequence of elements of the same data type, including logical, integer, double, character, complex, or raw types. Vectors can be accessed using indexing, and various functions like rep(), seq(), and is.vector() are available for manipulation and analysis. Key properties of vectors include their type, length, and attributes, with atomic vectors differing from lists in that all elements in a vector are of the same type.

Uploaded by

19A540 MAHESH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

VECTORS

WHAT IS VECTOR ?

A vector is a basic data structure which plays an important role in R


programming.

In R, a sequence of elements which share the same data type is known


as vector.

A vector supports logical, integer, double, character, complex, or raw


data type.
In R when values are appended with “L” as its suffix it means it is
an integer data type.
CLASS OF A VECTOR

We use the class( ) function to see what data type it is

Example:

class(myname)
[1] “character”

Both class() and typeof() appear to return very similar outputs on some vectors and lists
ELEMENTS OF A VECTOR

A vector supports logical, integer, double, character, complex, or


raw data type. The elements which are contained in vector known
as components of the vector or elements of vector.
HOW TO ACCESS THE ELEMENTS OF A VECTOR
VECTOR INDEXING
 Elements of a Vector are accessed using indexing.

 The [ ] brackets are used for indexing.

 Indexing starts with position 1.

 Giving a negative value in the index drops that element from

result.
> a <- c(1, 2, 5, 3, 6, -2, 4)
> a[3]
[1] 5
> a[c(1, 3, 5)]
[1] 1 5 6
> a[2:6]
[1] 2 5 3 6 -2
The colon operator used in the last statement is used to generate a sequence of
numbers. For example, a <- c(2:6) is equivalent to a <- c(2, 3, 4, 5, 6).
KEY PROPERTIES OF A VECTOR

1.Type: What it is. typeof()


2.Length: How many elements it contains. length()
3.Attributes: additional arbitrary metadata. attributes()

(Atomic vectors and lists are the building blocks for other important vector types like factors
and dates. I call these augmented vectors, because they are vectors with additional attributes,
including class. Because augmented vectors have a class, they behave differently to the atomic
vector on which they are built. E. g.
•Dates
•Date-times)
TYPES OF VECTORS

There is only one difference between atomic vectors and lists. In an atomic vector,
all the elements are of the same type, but in the list, the elements are of different
data types.
FUNCTIONS FOR VECTORS

1.rep()
2.seq()
3.is.vector()
4.as.vector()
5.any()
6.all()
7.lapply()
8.sapply()
The rep() Function
The rep() function repeats a vector, or value, a given number of times. It then returns a vector
with the repeated values.

Each Lengh.out
The seq() Function

The seq() function creates a sequence of numbers starting from the


given from argument and ending at the to argument. We can also
pass the by argument to specify the increase/decrease step. By
default, the increase step is 1.
The is.vector() Function

The is.vector() function takes an object as an input and returns TRUE


if the input object is a vector. It returns FALSE if the object is not a
vector.
The as.vector() Function

The as.vector() function takes an object as an argument and converts


it into a vector. Let’s take a look at the workings of this function with
an example.
First, let’s create a matrix. We will name this matrix mat_to_vec, as
we will be converting it into a vector soon.
The any() Function
The any() function takes a vector and a logical condition as input arguments. It
checks the vector against the condition and creates a logical vector. It then return
TRUE, if any one of the elements in the logical vector is TRUE.
The all() Function

The all() function takes a vector and a logical condition as input arguments. It
checks the vector against the condition and creates a logical vector. It then
returns TRUE if all the elements in the logical vector are TRUE, and FALSE if all
elements are not TRUE.
The lapply() Function
The lapply() function takes a vector, list or a data frame and a function. The
lapply() function applies a function to all elements of a vector, list or data
frames. The function then returns the result in the form of a list
The sapply() Function

The sapply() is very similar to the lappy() function. The only difference is
that the sapply() function returns the result in the form of a vector.
Arithmetic Operators
Operat Description Example
or
+ Adds two vectors v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t) it produces the following result −
[1] 10.0 8.5 10.0

− Subtracts second v <- c( 2,5.5,6)


vector from the first t <- c(8, 3, 4)
print(v-t) it produces the following result −
[1] -6.0 2.5 2.0
* Multiplies both v <- c( 2,5.5,6)
vectors t <- c(8, 3, 4)
print(v*t) it produces the following result −
[1] 16.0 16.5 24.0
/ Divide the first v <- c( 2,5.5,6)
vector with the t <- c(8, 3, 4)
second print(v/t)
When we execute the above code, it produces the following
result −
[1] 0.250000 1.833333 1.500000

%% Give the remainder v <- c( 2,5.5,6)


of the first vector t <- c(8, 3, 4)
with the second print(v%%t) it produces the following result −
[1] 2.0 2.5 2.0

%/% The result of v <- c( 2,5.5,6)


division of first t <- c(8, 3, 4)
vector with second print(v%/%t) it produces the following result −
(quotient) [1] 0 1 1
^ The first vector v <- c( 2,5.5,6)
raised to the t <- c(8, 3, 4)
exponent of second print(v^t) it produces the following result −
vector [1] 256.000 166.375 1296.000
Logical Operators
Operator Description Example
& It is called Element-wise Logical AND v <- c(3,1,TRUE,2+3i) t <- c(4,1,FALSE,2+3i)
operator. It combines each element of the first print(v&t)it produces the following result −
vector with the corresponding element of the [1] TRUE TRUE FALSE TRUE
second vector and gives a output TRUE if both
the elements are TRUE.
| It is called Element-wise Logical OR operator. v <- c(3,0,TRUE,2+2i) t <- c(4,0,FALSE,2+3i)
It combines each element of the first vector print(v|t)it produces the following result −
with the corresponding element of the second [1] TRUE FALSE TRUE TRUE
vector and gives a output TRUE if one the
elements is TRUE.
! It is called Logical NOT operator. Takes each v <- c(3,0,TRUE,2+2i) print(!v)it produces the
element of the vector and gives the opposite following result −
logical value. [1] FALSE TRUE FALSE FALSE

You might also like