# Vectors

# Vectors are homogenous data type which means at one instance you can store only one data type in

# be it numeric, character, logical

# In R there is no concept of Scalar, scalars on a conceptual level can be thought of vectors of length one

# Vectors can be created using two ways: Using c()function and vector() function

# Creating vectors using C:

x = c(1,2,3,4)

y = c(‘rohan’, ‘ram’, ‘ankit’)

z = c(TRUE, FALSE, TRUE)

# Creating vectors using vector():

x = vector(mode = “numeric”, length = 10)

y = vector(mode = “character”), length = 10)

# By default, if we do not specify ‘mode’ then mode is defined as logical,

z = vector(length = 10)

# Creating named vectors

marks = c(94,96,89,99)

names(marks) = c(‘Rohan’, ‘Ram’, ‘Ankit’, ‘Neha’)

# Creating a sequence vector using ‘:’ function

# Numeric sequence

0:9

9:0

# Practice – Examine the difference between the below output and explain why

x = 5

1:x-1

1:(x-1)

# Character sequence

LETTERS

letters

# Numeric vector sequence

x = c(1:35)

# Creating a vector using ‘seq’ function

x = seq(from = 10, to = 100, by = 10)

y = seq(from = 10, to = 100, length.out = 5)

LETTERS[seq(1,10)]

letters[seq(1,10,2)]

# Practice

# Use the seq function to generate the sequence 10,13,16,19

# Use the length.out parameter in seq function to generate a sequence of 8,16,32,40,48

# What is the output of the following code

seq(from = -10, to = 10, length.out = 5)

# In how many ways can you create vector c(1,2,3,4)

# Creating a vector using ‘rep()’ function

rep(3,4)

x = rep(c(3,4), 2)

# Create a vector with (1,2,3,4), I want the output to be (1,1,2,2,3,3,4,4)

x = rep(c(1,2,3,4), each = 2)

# Functions that can be performed on a vector

x = c(1,2,3,4)

# Find the length of x

length(x)

# Find the sum of x

sum(x)

# Find the mean of x

mean(x)

# Inspect the missing values

x = c(1,2,3,4,5, NA, 6, NA)

# Find the length of x

length(x)

# Find the mean of x

mean(x)

# Find the sum of x

sum(x)

# Remove missing values

# Sum of x without missing values

sum(x, na.rm = TRUE)

# Similarly for mean

# ——————-Write the Code——————————————————-

# Check if there are missing values in the vector

is.na(x)

# Count the number of missing values in a vector

sum(is.na(x))

x = c(1,4,5,2,3,NA, 2,3,4, NA, 1,4,5,2,3,NA, 2,3,4, NA, 1,4,9,19,NA, 2,3,4, NA, NA, 10,1,1,444,26.NA, 1222, 123, NA, NA)

———————————Write the code and output the number of missing values—————

===================================================================================================

# Adding and deleting elements in a vector

x = c(134,23,45,144,12)

# Now we want to add a number between 45 and 144 – say 199

x = c(x[1:3], 199, x[4:5])

===================================================================================================

# Vector Arithmetic

x = c(1,2,3,4)

y = c(2,3,4,5)

x+y

x*y

x/y

x = c(1,2,3,4)

x+4

x-4

x*4

x/4

# What if length is not one or the second vector is bigger or smaller than previous vector?? Concept of Recycling

x = c(1,2,3,4)

y = c(1,2,3)

z = c(1,2)

x+y

x*y

x+z

x*y

==================================================================================================

# Coercion in vector

x = c(1, 2, ‘ram’)

x = c(1, TRUE, FALSE)

x = c(‘ram’, TRUE, FALSE)

x = c(1, ‘ram’, TRUE)

x = c(1, 2, NA, TRUE)

x = c(1,2,NA, TRUE, ‘ram’)

# Ordering – logical < integer < numeric < complex < character

===================================================================================================

———————————–Subsetting Vectors——————————————-

# Subsetting using positions

x = c(144,20,45,12,67,09)

# Extract 2nd element

x[2]

# Extract 2nd to 5th element

x[2:5]

# Extract every element except the second element

x[-2]

# Extract every element except first three element

x[-(1:3)]

# Extract every element except the last element

x[-c(length(x-1):length(x))]

# Extract last two element using length function

x[-c(length(x)-2:length(x))]

# Extract last two element without using length function

tail(x,2)

# Extracting first two elements without using length function

head(x,2)

# Extract every element greater than 12

x[x>40]

# Extract every element less than or equal to 20

x[x<=20]

# ———————————–Subsetting using Names——————————-

# Write code to create marks of 5 students in a class and subset the marks using their names

——————————Filtering and comparing vectors—————————

x = c(20,23,44,23,12)

# Check if all of the elements have value less than 18

all(x<18)

# Check of all of the elements have value more than 10

all(x>10)

# Check if any element of the vector have value greater than 25

any(x>25)

# Check if any element of the vector have value less than 10

any(x<10)

# Check for below code, understand the difference between the two

x>8

x[x>8]

# Selection function

which(x*x>23)

—————————–Sorting/Ordering vectors in R—————————–

x = c(10,8,14,12,19,21,3,4)

sort(x)

# Sorting in reverse order

sort(x, decreasing = TRUE)

# Sorting if NA present

x[2] = NA

x

sort(x)

# —————————-Check for equality amongst vectors—————————

x = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

y = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

identical(x,y)

x = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

y = c(TRUE, TRUE, FALSE, TRUE, NA, TRUE, TRUE)

identical(x,y)

x = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

y = c(TRUE, TRUE, FALSE, NA, FALSE, TRUE, TRUE)

identical(x,y)

x = c(TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE)

y = c(TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE)

# Check which element is unequal

which(x != y)

x = c(1,2,3,NA)

y = c(1,2,3,NA)

identical(x,y)

x = c(NA, NA)

y = c(NA, NA)

identical(x,y)

x= c(1,2,3,NULL)

y = c(1,2,3,NULL)

identical(x,y)

# —————————Check for Equality using == operator——————————

x = c(1:5)

y = c(1:5)

x == y

x = c(1,2,3,NA,5)

y = c(1,2,3,4,5)

x == y

x = c(2,1,4,5,2,7)

y = c(2,1,4,5,2,7,2)

identical(x,y)

x == y

# Similary you can perform >, < , >=, <= on vectors

# ————————-Excercises on Vectors—————————————-

1. Consider a vector:

x = c(10,8,14,12,19,21,3,4)

What is the value of:

c(10,8,14,12,19,21,3,4) >4

2. a = c(20,49,43,23,234,45)

b = c(11,34,23)

What is the value of: a+b

3. x <- c(34, NA, 55, 67, NA, 4, 77, NA, 11, NA, 39)

# Write the code to count the number of NA in x

# ——————————————————————————————–

# Count the number of times an element occur in a vector

x = c(1,2,1,4,2,4,1,5,6,4,3,5,2)

table(x)

# Find the min and max element in a vector

a = c(10,12,6,89,12,45,422,12,90,34)

min(a)

max(a)

# Find the position of min and max element in a vector

which.min(a)

which.max(a)

# Find if a number is a present in vector

a = c(2,3,2,4,2,5,6,4,1)

a %in% 1

sum(a %in% 1)

# Find at which position the number is present in a vector

which(a %in% c(2,3))

# Insert elements in vector beyond the existing length

x = c(1,2,3,4,5)

x[9] = 9

x

## 1 thought on “Vectors – Basic Building Blocks of R”