R Vector – Types and R Vector Operations with Examples
1. R Vector – Objective
In our Previous R tutorial, we have discussed Data Types in R. Today, we are going to discuss the R Vector data type in detail. Moreover, we will cover what is Vector in R, what are the types of vector? Also, we will discuss various types of operation performed on R Vector with examples.
So, let’s start the R Vector tutorial.
2. What is R Vector?
The basic data structure in R is the vector. R vector comes in two parts: Atomic vectors and Lists. They have three common properties:
- Type function, what it is?
- Length function, how many elements it contains.
- Attribute function, extra arbitrary metadata.
They differ in the types of their elements: All elements of an atomic vector must be the same type, whereas the elements of a list can have different types. We have discussed R Lists in our previous blog, Here we are going to discuss only Atomic Vectors.
i. Atomic Vectors
There are four common types of R Atomic Vectors:
- Numeric Data Type
- Integer Data Type
- Character Data Type
- Logical Data Type
a. Numeric Data Type
Decimal values referred as numeric data types in R. If we assign a decimal value for any variable g like given below, g will become a numeric type.
g = 53.5 #assign a decimal to g g # print the variable’s value – g
b. Integer Data Type
A numeric value with no fraction called integer data, represented by “Int”. Some examples of an integer are -54 and 23. Int size is 2 bytes while long Int size is 4 byte.
Int a: 20 Int b: 30 Int c: 40
c. Character Data Type
The char held as the one-byte integer in memory and it also held as a one-byte integer in memory
char grade, symbol; Grade = “B”; Symbol = “$”
d. Logical Data Type
It is been created when a comparison between variables exist.
a =3; b =6 #sample values g = a>b # is a larger than b? g #print the logical value
Read: R Data Types – Vectors, Matrices, Lists, and Data Frames
3. Vector Operations in R
i. Combining Vector
Functions are used to combine vectors.
Vectors n and s combine into a new vector containing elements from both the vectors.
n = c (1, 3, 8) s = s (p, q, r) C (n, s)  “1”, “3”, “8”, “p”, “q”, “r”
ii. Vector Arithmetic
Arithmetic operations on vectors can be performed member-by-member.
Suppose we have two vectors a and b:
a = c (1, 3) b = c (1, 3)
If we add, subtract, multiply and divide a and b
a + b  2, 6 a – b  0, 0 a / b  1, 9 a % b  1, 1
iii. Logical Index Vector in R
By using a logical index vector in Rwe can form a new vector from a given vector, which has the same length as the Original vector. If the corresponding members of the original vector include in the slice, then vector members are TRUE and otherwise FALSE.
Following vector S of length 2
S = c ("bb", "cc”)
We define a logical vector L of the same length to retrieve the first and second members of S and have its first and second members set as TRUE
L = c (TRUE, TRUE) S [L]  “bb” “cc”
iv. Numeric Index
By using a numeric index vector, we can form a new vector from a given vector. Hence, it shows here how to retrieve a vector slice containing the second and fourth members of a given vectors.
s = c ( “aa” , “bb” , “cc” , “dd” , “ee”) s = [ c(2, 4) ]  “bb” “dd”
v. Duplicate Index
The index vector allows duplicate values. Hence the following retrieves a member twice in one operation.
s [ c(2, 3, 3) ]  "bb" "cc" "cc"
vi. Range Indexes
To produce a vector slice between two indexes, we can use the colon operator “:“. This is convenient for situations involving large vectors.
S   "aa" "bb" "cc"
Read: Data Structures in R | A Cheat Sheet to R Data Structures
vii. Out-of-order Indexes
The index vector can even be out-of-order. Here is a vector slice with the order of first and second members reversed.
S [ c (2, 1, 3) ]  "bb" "aa" "cc"
viii. Named Vectors Members
We can assign name to vector member
v= c (“Aarti”, “Joshi”) v  “Aarti” “Joshi”
Now we can name the first member as First, and the second as Last.
Names (v) = c (“first”, “Last”) V First Last “Aarti” “Joshi”
Then we can retrieve the first member by its name.
v[“First”]  “Aarti”
Furthermore, by using a character string index vector we can reverse the order
V [c ("Last", "First”) Last First "Joshi" "Aarti"
So, this was all on R vector tutorial. Hope you like the R language vector tutorial.
Read:R Lists – How to Create, Access, Manipulate, Merge Lists in R?
4. Conclusion – R Vector
Hence, we have studied the R vector in detail. Also, we have mentioned the different type of vectors and how to use it. So, this above information will surely help the person who is not so much aware of vectors and their uses. Still, you have any query in R vector, please comment us in a section given below. We will be glad to solve your doubts.