1) Numeric vector:

1.1 Vector having numbers are called numeric vectors. Individual numbers are actually one-element vectors.

x<-8 # Assign value 8 to variable object x
print(x)
## [1] 8
class(x) # To display the class or data type of x.
## [1] "numeric"

1.2 let’s create a vector of 4 numbers.

y<-c(10,12,13,15) # "c" is concatenate which is used for concatenating set of one-element vectors.
y
## [1] 10 12 13 15

Note that all elements are of same type.

1.3 Accessing elements from a vectors.

1.3.1 Accessing (or) Sub setting single element from a vector

y<-c(10,12,13,15)
y[1] #Accessing first element of the vector.
## [1] 10

Note that in R language, the indexing starts from 1 and not 0(unlike python and c).

1.3.2 Accessing 2nd, 3rd and 4th element from a vector

y<-c(10,12,13,15)
y[2:4] 
## [1] 12 13 15

1.3.3 Accessing 1st and 3rd element from the vector

y<-c(10,12,13,15)
y[c(1,3)] 
## [1] 10 13

1.3.4 Accessing 1st element 2 times and the 3rd element

y<-c(10,12,13,15)
y[c(1,1,3)]
## [1] 10 10 13

1.3.5 Accessing elements excluding second element.

Negative subscripts mean that we want to exclude the given elements in our output.

y<-c(10,12,13,15)
y[-2] 
## [1] 10 13 15

1.3.6 Accessing elements excluding 1st,2nd and 3rd elements.

y<-c(10,12,13,15)
y[-1:-3] 
## [1] 15

1.3.7 Returns the length of a vector

y<-c(10,12,13,15)
length(y)  
## [1] 4

1.3.8 Returns the vector by removing the last element

y<-c(10,12,13,15)
y[-length(y)] # Here the length of y is 4. i.e y[-4]. So, we are removing last element from the vector y.
## [1] 10 12 13



1.4 Other way of creating a vector

1.4.1 Using the function vector()

x<-vector(length = 2) # Specify what data type and its length
x[1]<-44
x[2]<-33
x
## [1] 44 33

1.4.2 Creating vectors with operator “:”

z<-3:10 # Generate numbers in the range.
z
## [1]  3  4  5  6  7  8  9 10

1.4.3 Creating vectors with sequence

z<- seq(from=4,to=16, by=2) # Generates numbers from 4 to 16 with intervel of 2
z
## [1]  4  6  8 10 12 14 16

1.4.4 Another was of creating vectors with sequence

z<- seq(from=10, to=20, length=3) # 3 elements are generated in the range 10 to 20 with equal interval 
z
## [1] 10 15 20

1.4.5 The rep() (or repeat) function allows us to conveniently put the same constant into long vectors. The call form is rep(x,times), which creates a vector of times*length(x) elements.

z<- rep(8,4) # Repeat number 8 4 times.
z
## [1] 8 8 8 8

1.4.6 Another way of using repeat

z<-rep(c(5,6,7),each=2) # each element in the vector is repeated 2 times
z
## [1] 5 5 6 6 7 7



1.5 Some common operations on vectors.

1.5.1 Add two vectors of different lengths- Recycling

When applying an operation to two vectors that requires them to be the same length, R automatically recycles, or repeats, the shorter one, until it is long enough to match the longer one.

x<-c(1,2,3,4)
y<-c(5,6,7,8,9,10)
x+y
## Warning in x + y: longer object length is not a multiple of shorter object
## length
## [1]  6  8 10 12 10 12

1.5.2 Using all() and any()

The any() and all() functions are handy shortcuts. They report whether any or all of their arguments are TRUE.

z<- c(1,3,5,6,7) # Returns true, if any number in vector z are greater than 3.
any(z>3)
## [1] TRUE
z<- c(1,3,5,6,7)
all(z>3) # Returns true, if all elements in vector z are greater than 3.
## [1] FALSE

1.5.3 NA and Null Values

In statistical data sets, we often encounter missing data, which we represent in R with the value NA. NULL, on the other hand, represents that the value in question simply does not exist, rather than being existent but unknown.

x<-c(1,3,6,5,7,NA,6) # This vector contains one NA
mean(x) # If you try to caliculate mean of x, you will get NA. Which is not desired.
## [1] NA

Remove NA to calculate the mean

mean(x, na.rm = TRUE)
## [1] 4.666667

let’s calculate length of NA

p<-NA
length(p) # The answer is 1
## [1] 1

Now, Calculate the length of NULL

p<-NULL
length(p)# The answer is 0- Null means, it does not exists.
## [1] 0

Practically, Null is used to initialize a variable to zero.

1.6 Filtering

1.6.1 Traditional way of filtering

This allows us to extract a vector’s elements that satisfy certain conditions.

z<-c(1,3,4,7,9) # Define a vector
j<-z*z>8 # calculate square of every element and find if they are greater than 8.
print(j) # The output of j is in the form of logical-TRUE and FALSE
## [1] FALSE  TRUE  TRUE  TRUE  TRUE
z[j]# Displays the numbers whose square is greater than 8
## [1] 3 4 7 9

1.6.2 Filtering using which() function.

z<-c(1,3,4,7,9)
which(z*z>8) # Returns the indexes of elements following the condition.
## [1] 2 3 4 5