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

R Programming Vector Notes

The document provides an overview of data structures in R programming, focusing on vectors as a fundamental data type. It explains how to create, access, and manipulate vectors, including operations like combining, arithmetic, and indexing. Additionally, it covers coercion of data types within vectors and various methods for modifying and deleting vector elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

R Programming Vector Notes

The document provides an overview of data structures in R programming, focusing on vectors as a fundamental data type. It explains how to create, access, and manipulate vectors, including operations like combining, arithmetic, and indexing. Additionally, it covers coercion of data types within vectors and various methods for modifying and deleting vector elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

DATA STRUCTURE/VECTOR

Data structures in R programming provide a way to organise and store data


efficiently. Data structure is a way to stored data in memory. Data structure
play important role to perform any operations and retrieve data. Data
structures in R programming are tools for holding multiple values
R base data structures are often organized by their dimensionality(1D,2D,3D)
weather they are homogenous or heterogenous. This gives rise to the six data
types which are most frequently utilize in data analysis.
The most essential data structures used in R are as following
1)VECTORS
2)LISTS
3)DATA frame
4)MATRICE
5)array
6)FACTORS
***********************************************************
1) Vector: Vector is one of the basic data structures in R. It is homogenous,
which means it only contains element of same data types. Data type can be
numeric, integer, character, complex or logical.
Vectors are basically elements which share the same data types. Vectors
are one dimensional data structure. And it is a collection of homogenous
data.
i) Creating Vector
(ii) Accessing element vector
(iii) Vector operation
i) Creating Vector: Vectors are created by using the c() function. Coercion
takes place in a vector from bottom to top. If the element passed are of
different types from logical to integer to integer to double to character.
Example:
v1<-c("Ramm","Teena","Ravi")
v1
V2<-c(1,"Ram",FALSE,9.2)
V2
x<-c(1,2,3,4,"a")
x
x<-c(1,2,3,TRUE)
x
Note:
Coercion: When you call a function with an argument of the wrong type, R will try to
coerce values to a different type so that the function will work.

So how does R coerce data types? If a character string is present in an atomic vector, R will
convert everything else in the vector to character strings. If a vector only contains logical
and numbers, R will convert the logical to numbers; every TRUE becomes a 1, and
every FALSE becomes a 0, as shown below.
Example1:
V2<-c(1L,"Ram",FALSE,9.3)
V2
Answer: "1" "Ram" "FALSE" "9.3"
Explanation: In the above example Ram is a character data type and it can not
be convert into integer data type, logical data type or Numeric data type,
In that case Character data type(Ram) coerced all the data type(1L,FALSE,9.3)
into Character data type and it makes all the element in same data types.
Example2:
b<-c(2+3i,TRUE,7.8)
b
Answer: 2.0+3i 1.0+0i 7.8+0i

Explanation: In the above example 2+3i is a complex data type and it can not
be convert logical data type or Numeric data type, but logical data and
numeric data type can be converted into complex data type
In that case Complex data type(2+3i) coerced all the data type(TRUE,7.8) into
complex data type and it make all the element in same data types.
Example-3
Z<-c(1L,20,-13)
Z
Answer3 1 20 -13

Explanation: In the above example 20,-13 is a Numeric data type and it can not
be convert into lnteger data type, but lnteger data type can be converted into
numeric data type.
In that case Numeric data type(20,-13) coerced all the data type(1L) into
numeric data type and it make all the element in same data types.
Atomic vector: Atomic vector are the most basic objects in R as they can
contain only data of one type. There are 5 types of atomic vector in R
i)Numeric
ii)Integer
iii)Character
iv)Logical
v) Complex
********************************************************
i) Numeric
EXAMPLE
vec<-c(13.3,54,34.7,89)
vec
class(vec)

ii)integer
EXAMPLE
int<-c(13.3,54,34.7,89)
int1<-as.integer(int)
int1
class(int1)

ii)Character:
EXAMPLE
vec<-c(13.3,54,34.7,89)
vec1<-as.character(vec)
vec1

vec1<-c("Ram","Mohan","Teena")
vec1
iv)Logical
EXAMPLE
logi<-c(TRUE,FALSE)
logi
v)Complex
EXAMPLE
vec1<-c(2+2i, 2-1i)
vec1

vi)Length
EXAMPLE
x<-c(1,4,5,8,9,2,0)
length(x)

y<-c("Apple","Banan","Grapes")
length(y)

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

2)Accessing element of vector: Accessing element in a vector is the process of


performing operations on an individual element of vector. There are many
ways through which we can assess the elements of the vector. The most
common is using “[]” (Square bracket) symbol.
Elements of a vector can be accessed by using their respective index, bracket
are used to specify indexes of the element
Note: Indexing in R Start from 1, But indexing in C, C++, java Start from 0
i)Indexing with character vector
EXAMPLE
x<-c("Jan","Feb","March","Apr","May","June")
y<-x[c(3,6,2)]
y

ii)Indexing with Logical vector: In logical indexing it returns the values of the
position whose corresponding position has a logical vector TRUE
EXAMPLE
a<-c(1,2,3,5,6,7)
b<-a[c(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE)]
b

iii)Indexing with integer vector:


EXAMPLE
z<-seq(1,4,length.out=6)
[Note : when we use length.out function to add decimal value then no need
to assign decimal with number]
z
z[3]
z1<-seq(1,5,by=.5)
z1
[Note: when we use by function to add decimal value then we need to assign
decimal with number]

3)Vector operation: In R there are various operations which is performed on


the vector. It can play important role and operations are required for data
manipulation, There are some vector operation are describe below.

1)combining vector: The c() function is not only used to create a vector, but
also it is used to combine two vectors, By combining one or more vectors it
forms a new vector which contains all the element of each vector.

EXAMPLE
a<-c(1,2,3,4,5)
b<-c("Ram","Shayam","Mohan","Sohan")
z<-c(a,b)
z
2)Arithmetic Operation: By using arithmetic operation we can perform some
calculation.
EXAMPLE
a1<-c(1,2,3,4,5,6,7)
a2<-c(5,6,7,8,9,1,2)

x1<-(a1+a2)
x1
x2<-(a1-a2)
x2
x3<-(a1*a2)
x3
x4<-(a1/a2)
x4
x5<-(a1%%a2)
x5
x6<-(a1%/%a2)
x6
x7<-(a1^a2)
x7
3)Logical Index vector: With the help of logical index vector in R we can form a
given vector. This vector has the same length as the original vector. The vector
member are TRUE only when the corresponding members of the original
vector are included in the slice otherwise it will be FALSE.
EXAMPLE
b1<-c("Ram","Shayam","Mohan","Sohan","Priya","Teena")
b2<-c(TRUE,FALSE,FALSE,TRUE,FALSE,TRUE)
Z<-b1[b2]
Z
4)Numeric Index: In R we specify the index between square braces [] for
indexing a numerical value .
a)positive index: If index is positive. It will return only that value which we
have specified in Index.
EXAMPLE
b1<-c("Ram","Shayam","Mohan","Sohan","Priya","Teena")
b2<-b1[3]
b2
b)Negative index: If index is negative. It will return all the values except for the
index which we have specified.
EXAMPLE
b3<-b1[c(-5,-2)]
b3
5)Duplicate Index: An index vector allow duplicate values which means we can
access one element twice in one operation. Means if we assign two same value
then return duplicate value.
EXAMPLE
b1<-c("Ram","Shayam","Mohan","Sohan","Priya","Teena")
b2<-b1[c(2,4,5,2)]
b2
6)Range indexes: Range index is used to slice our vector to form a new vector.
For slicing we used colon(:). Range index are very helpful for situation involving
a lager operator .
EXAMPLE
a1<-c("Ram","Shayam","Mohan","Sohan","Priya","Teena")
a2<-a1[c(2:5)]
a2

a3<-a1[1:4]
a3
7)Out of order: If we want any specific elements then we can extract by
assigning a position of elements
EXAMPLE
b1<-c("Ram","Shayam","Mohan","Sohan","Priya","Teena")
b2<-b1[c(2,3,1,5)]
b2
8)Named vector: Name vector is used to allocate the name of vector.
EXAMPLE
Z<-c("Tensorflow","Pytorch")
names(Z)=c("Start","End")
Z["Start"]

z<-c("Ram","Shayam","Mohan","Sohan")
names(z)<-c("Y1","Y2","Y3","Y4")
z
z["Y1"]
z["Y2"]
9)Modifying Vector: Modification of 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 way through which we can modify a vector.
If we want to replace any element by another element then we can replace by
following method.
EXAMPLE
x<-c(2,3,5,6,8)
x[3]<-1
x
x[5]<-9
x
a<- c("Ram","Shayam","Mohan","Sohan")
a[2]<-“Teena”
10)Deleting vector[NULL]: Deleting R vector is the process of deleting all of the
elements of the vector. This can be done by assigning it to a NULL value.
EXAMPLE
x<-c(2,3,5,6,8)
x<-NULL
x
11)Sorting vector: Sort function is used to arrange the value in ascending or
Descending order.
EXAMPLE
z<-c(2,6,1,8,3,9,3)
v<-sort(z)
v
Note: If we directly apply sort function by default elements arrange in
ascending order.

b<-sort(z,decreasing=TRUE)
b
b<-sort(z,decreasing=FALSE)
b

You might also like