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

Vectors: Assistant Professor Department of Cse VFSTR Deemed To Be University Guntur, Ap, India

This document discusses vectors in R. It defines a vector as a collection of elements and describes how to create single and multiple element vectors using c(), :, and seq() functions. It explains the different types of vectors - double, integer, character, logical, complex, and raw. The document also covers arithmetic operations on vectors like addition, subtraction, multiplication, and division. Finally, it discusses how to access vector elements using indexes, ranges, negative indexes, and Boolean values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Vectors: Assistant Professor Department of Cse VFSTR Deemed To Be University Guntur, Ap, India

This document discusses vectors in R. It defines a vector as a collection of elements and describes how to create single and multiple element vectors using c(), :, and seq() functions. It explains the different types of vectors - double, integer, character, logical, complex, and raw. The document also covers arithmetic operations on vectors like addition, subtraction, multiplication, and division. Finally, it discusses how to access vector elements using indexes, ranges, negative indexes, and Boolean values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Vectors

BY
B SUVARNA
ASSISTANT PROFESSOR
DEPARTMENT OF CSE
VFSTR DEEMED TO BE UNIVERSITY
GUNTUR, AP, INDIA

Department of Computer Science & Engineering


contents
1. What is vector
2. Vector creation
Single element vector
Multiple element vector
3. Types of vectors
4.Arithmetic operations on vectors
5. Accessing the elements of vectors

Department of Computer Science & Engineering


outcomes
At the end of this lecture yow will be
able to
•create a vector
•Perform mathematical operations
on vectors
•accessing the elements

Department of Computer Science & Engineering


Introduction: Vectors
Vector: Vector is a collection of elements or
set of elements.
•The memory used by the vector can be
increased or decreased as elements are
added or removed from the vector.

Department of Computer Science & Engineering


Introduction: Vectors
EX1:
A=C(1,3,4,5,6,10)

EX2:
B=C(“H”,”E”,”L”,”L”,”O”)

EX3:
C=C(“SUN”,”MON”,”TUE”,”WED”,”THU”,”FRI”,”SAT”)

Department of Computer Science & Engineering


Vector Creation
A vector can be created by giving values to variable.
Syntax:
var_name=value #single element vector

var_name=c( set of values) #Multiple element


vectors

1. Single element Vector


Ex: a=10
2. Multiple elements Vectors
Ex: b=c(1,3,5,6,7,9,10)

Department of Computer Science & Engineering


Vector Creation: Single
element Vector
Syntax: variable_name<-value
Ex:
a<-10 #double
b<-”x” #character
c<-1L #Integer
d<-True #logical
e<-2+3i #complex

Department of Computer Science & Engineering


Vector Creation: Multiple element Vector

Syntax: var_name=c( set of values)

Ex1: a=c(1,3,4,5,6,10)
Create Multiple vectors by using 3 ways
1. : operator
2. seq() function
3. c() function
: operator
Syn: var_name=start_number:ending_number
Ex: a<-10:15
b<-10.2:15.7

Department of Computer Science & Engineering


Multiple elements Vector: seq()
function
syntax: seq(start, end, by=value)
Examples
 seq(2,10)
 seq(2,10,by=2)
 seq(3,30,by=3)

Department of Computer Science & Engineering


Multiple elements Vector: c()
function
Multiple vectors creation: using c() functio
Syntax:var_name<-c(num1,num2,--numn)
Example
a=c(1,3,4,5,6,10)
b=c(“h”,”e”,”l”,”l”,”o”)

Department of Computer Science & Engineering


Types of vectors

THERE ARE SIX TYPES OF ATOMIC VECTORS.


• DOUBLE
•INTEGER
• CHARACTER
•LOGICAL
•COMPLEX
• RAW.

Department of Computer Science & Engineering


Vector Creation: Double
Double:
Var_name=number
die <- c(1, 2, 3, 4, 5, 6)
a <- c(10, 20, 30, 40, 50, 60)
Typeof(a)

Department of Computer Science & Engineering


Vector Creation: Integer
Integer
Var_name=number L
a<- c(-1L, 2L, 4L)
Typeof(a)

Department of Computer Science & Engineering


Vector Creation: character
Character
Var_name=“character”
a<- c("Hello", "World")
## "Hello" "World"
typeof(a)
## "character"
typeof("Hello")
## "character"

Department of Computer Science & Engineering


Vector Creation: Logical
Logical-TRUE,FALSE
Useful in comparisons
3>4
20<16
A<-c(TRUE,FALSE,TRUE)

Department of Computer Science & Engineering


Vector Creation: complex
Add an imaginary part to a number
with i
Synatx:
Ex
A=2i
B=c(1+2i,2+2i,3+4i)

Department of Computer Science & Engineering


Vector Creation: raw
Stores raw bytes of the data.
syntax: var_name=raw(n)
Ex: a=raw(2)
00 00

Department of Computer Science & Engineering


Operations on Vectors
Operations that can be done on vectors are
1. Arithmetic operations
• Addition
• Subtraction
• Multiplication
• Division
2. Accessing Elements

Department of Computer Science & Engineering


Arithmetic operations:
Addition
To do operations on vectors the length of th
vectors must be same:
>a=c(1,3,4,5,6)
>b=c(2,3,4,5,8)
>a+b

Department of Computer Science & Engineering


Arithmetic operations: sub,mul,div

Subtraction
>a=c(1,3,4,5,6)
>b=c(2,3,4,5,8)
>a-b
Multiplication
>a=c(1,3,4,5,6)
>b=c(2,3,4,5,8)
>a*b
Division
>a=c(1,3,4,5,6)
>b=c(2,3,4,5,8)
>a/b

Department of Computer Science & Engineering


Arithmetic operations
>a=c(5,10,15,20,25,30)
>b=c(2,4)
>a+b
The elements of the shorter vector are
recycled
• Vector b becomes
b=c(2,4,2,4,2,4)

Department of Computer Science & Engineering


Arithmetic operations
>a=c(2,3,10,15,20,25)
>b=c(2,4)
>a+b
B has recycled
b=c(2,4,2,4,2,4)
A+b=(4,7,12,19,22,29)

Department of Computer Science & Engineering


So far….
1. Vector creation
2. Types of vectors
3. Arithmetic operations on vectors
4. Accessing the elements of vectors

Department of Computer Science & Engineering


2. Accessing vectors
Vector elements are accessed using []
Note: Starts with position 1.

1. Array starts from 0


2. Vectors starts from 1

Department of Computer Science & Engineering


Accessing vectors

•Single element
•Multiple elements
•Negative indexing
•Booleans

Department of Computer Science & Engineering


Accessing vectors: Single element

Syntax: array_name[position]
Ex1:
a=c(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”F
ri”,”Sat”)

Ex2:
a=c(10,20,30,40,50,60,70)

Department of Computer Science & Engineering


Accessing vectors: Multiple
elements

To access multiple elements use : operator


Syntax: array_name[start_pos:end_pos]
Ex1:
a=c(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”)
>a[2:5]

Department of Computer Science & Engineering


Accessing vectors: with negative
values
Vector elements can also be accessed using
negative values
Syntax: array_name[-negative indexing value]
a=c(10,20,30,40,50,60,70)
if you want to print all the elements except 10
then
>a[-1]

>a[-2]

Department of Computer Science & Engineering


Accessing vectors: with Booleans

Vector elements can also be accessed using


Boolean values
Syntax: array_name[TRUE or FALSE]
Ex
>a=c(10,20,30,40,50,60,70)
>a[c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE)]

Department of Computer Science & Engineering


Accessing vectors: with Booleans

THANK YOU

Department of Computer Science & Engineering

You might also like