Chapter 8 Vectors

In this chapter we will cover vectors. A vector is a data structure in R. A data structure can be defined as a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. A vector can either be an atomic vector or a list. An atomic vector contains only one data type whereas a list may contain multiple data types.

Atomic vector could be Character, Logical, double, integer or complex

# Integer
4 
## [1] 4
# Character
"Pune"
## [1] "Pune"
#You can use typeof function to check the type of each of these
typeof("Pune")
## [1] "character"
typeof(4)
## [1] "double"
typeof(4.3)
## [1] "double"
typeof(2+1i) 
## [1] "complex"

8.1 Creating a Vector

Lets create a vector containing three numeric values. This will be an example of Numeric Vector

c(1,4,7)
## [1] 1 4 7
# To check length of the vector we can use length function. 
length(c(1,4,7))
## [1] 3
#Lets assign this to a variable for future use
Num_variable<-c(1,4,7)

Let’s create a vector “Names” that contain names of person appearing in an Exam. This will be a kind of character vector. Please note that here “Names” is a vector and“<-” is an operator that assigns value on the right hand side of the operator to the variable name in the left hand side. The character “c” is used to create vector

Names<-c("Arvind", "Krishna", "Rahul", "Saurabh", "Venkat","Sucharitha")

# Lets extract first element of Names vector. To extract first element we need to use "[" and 1 value
Names[1]
## [1] "Arvind"
#Similarly to extract third value from Names vector.

Names[3]
## [1] "Rahul"

Lets create another vector and see if we can combine two vectors

Missed_names<-c("Ajay","Amit")
# Here we will be updating Names vector by including Missed_names in the Names vector.

Names<-c(Names,Missed_names)
Names
## [1] "Arvind"     "Krishna"    "Rahul"      "Saurabh"    "Venkat"    
## [6] "Sucharitha" "Ajay"       "Amit"

8.2 Class of Vector

We can also check the types of vector using different functions such as

class(Names)
## [1] "character"
class(Num_variable)
## [1] "numeric"
typeof(Num_variable)
## [1] "double"

What is the difference between typeof vs mode vs class? I will leave it to reader to explore it on their own. Remember the reader can use help function to check the definitions in details for example just type “?class” in the console to see the details of class function.

8.3 Adding Vectors of different type

Let’s add character and numeric vector and check the result

mix_vector<- c(1,2,"Amish")
class(mix_vector)
## [1] "character"

The mix_vector we created had multiple data types-Numeric and Character, however R converts the multiple data type to a single data type through a process called coercion. Logical values are converted to numbers: TRUE is converted to 1 and FALSE to 0.

Values are converted to the simplest type required to represent all information.

The ordering is roughly logical < integer < numeric < complex < character < list.

Objects of type raw are not converted to other types.

Objects can also be explicitly coerced using “as.”" function. For example to coerce mix vector to numeric use:-

mix_vector<- as.numeric(mix_vector)
## Warning: NAs introduced by coercion
mix_vector
## [1]  1  2 NA
class(mix_vector)
## [1] "numeric"

We can also change the elements of the vector. Let’s change the first element of the Names vector we can use following expression

Names[1]<-"Arun"
Names
## [1] "Arun"       "Krishna"    "Rahul"      "Saurabh"    "Venkat"    
## [6] "Sucharitha" "Ajay"       "Amit"

8.4 Accessing elements of vectors

Let’s try to access elements of vector using negative indexing.

Names[-1] # All Elements except the first element will be printed
## [1] "Krishna"    "Rahul"      "Saurabh"    "Venkat"     "Sucharitha"
## [6] "Ajay"       "Amit"
Names[c(-1,-2)] # All Elements except the first and second elements will be printed
## [1] "Rahul"      "Saurabh"    "Venkat"     "Sucharitha" "Ajay"      
## [6] "Amit"

Alternatively, we can also use following code to remove first two elements of Names vector

Names[-c(1,2)] # All Elements except the first and second elements will be printed, we just used negative at the start of vector , rather than using negatives in front of multiple numbers.
## [1] "Rahul"      "Saurabh"    "Venkat"     "Sucharitha" "Ajay"      
## [6] "Amit"

8.5 Creating Vector using In Built Functions

seq(1,10, by = 1) # Create a sequential vector from 1 to 10 which increases by 1 in length.
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(1,10, by = 0.5)
##  [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5
## [15]  8.0  8.5  9.0  9.5 10.0
seq(1,10, by = 0.5)
##  [1]  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5
## [15]  8.0  8.5  9.0  9.5 10.0

8.6 Let’s try to repeat vectors

rep(c(1, 2), times = c(5,5))
##  [1] 1 1 1 1 1 2 2 2 2 2

typeof(Vector): This method tells you the data type of the vector.

Sort(Vector): This method helps us to sort the items in the Ascending order.

length(Vector): This method counts the number of elements in a vector.

head(Vector, limit): This method return the top six elements (if you Omit the limit). If you specify the limit as 4 then, it returns the first 4 elements.

tail(Vector, limit): It returns the last six elements (if you Omit the limit). If you specify the limit as 2, then it returns the last two elements.

8.6.1 Arithmetic Operator in Vectors in R

We have performed arithmetic operations in previous chapters. Let’s see how can we perform arithmetic operations in Vectors.

V1<- c(1,2,3,4) # created a vector V1 
V2<- c(5,6,7,8) # created a vector V2

Lets perform addition

V12<-V1+V2
V12 #Print result of V12, which is sum of V1 and V2 vector.
## [1]  6  8 10 12

As you may have guessed the addition operation above was performed elementwise. That means that first element of V1 was added to first element of V2

Let’s perform multiplication

V12_mult<-V1*V2
V12_mult
## [1]  5 12 21 32

Lets perform division

V12_division <-V2/V1
V12_division
## [1] 5.000000 3.000000 2.333333 2.000000
typeof(V12_division)
## [1] "double"

Lets convert the double to numeric

V12_division<-as.integer(V12_division)
V12_division
## [1] 5 3 2 2
typeof(V12_division)
## [1] "integer"