3. A vector is the simplest type of data
structure in R. Simply put, a vector
is a sequence of data elements of the
same basic type. It is used to store
multiple measurements of the same
type.
(e.g. data variables)
Scalars :
Arrays :
Matrices :
Vectors :
A scalar object is just a single value like a number or a name.
(e.g. a <- 100 b <- 3 / 100 c <- (a + b) / b.)
The array is objects that can hold two or more than two-dimensional data.
A matrix is a two dimensional data set
with columns and rows.
4. Table of contents
Adding and Deleting
Vector Elements
01 02
Obtaining the
Length of a Vector
03
Matrices and Arrays
as Vectors
5. Adding and Deleting Vector Elements
• Vectors are stored like arrays in C.
For example,
> x <- c(88,5,12,13)
> x <- c(x[1:3],168,x[4]) # insert 168 before the 13
> x
[1] 88 5 12 168 13
6. Obtaining the Length of a Vector
You can obtain the length of a vector by using the length() function:
x <- c(1,2,4)
> length(x)
[1] 3
Without the length() function,
for (n in x)
The problem with this approach is that it doesn’t allow us to retrieve the
index of the desired element.
> x <- c()
> x
NULL
> length(x)
[1] 0
> 1:length(x)
[1] 1 0
7. Matrices and Arrays as Vectors
Arrays and matrices (and even lists, in a sense) are actually vectors too,
For example,
> m
[,1] [,2]
[1,] 1 2
[2,] 3 4
> m + 10:13
[,1] [,2]
[1,] 11 14
[2,] 14 17
9. Declare variables in R,
z <- 3
For instance, say we wish y to be a two-component vector with values 5 and 12.
> y[1] <- 5
> y[2] <- 12
> y <- vector(length=2)
> y[1] <- 5
> y[2] <- 12
> y <- c(5,12)
11. 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.
> c(1,2,4) + c(6,0,9,20,22)
[1] 7 2 13 21 24
Here’s a more subtle example:
> x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> x+ c(1,2)
[,1] [,2]
[1,] 2 6
[2,] 4 6
[3,] 4 8
1 4
2 5
3 6
1 2
2 1
1 2
• x + c(1,2,1,2,1,2)
+
13. Table of contents
Vector Arithmetic and
Logical Operations
01 02
Vector Indexing
03
Generating Useful Vectors
with the : Operator
Generating Vector
Sequences with seq()
04 05
Repeating Vector
Constants with rep()
14. Vector Arithmetic and Logical Operations
> 2+3
[1] 5
> "+"(2,3)
[1] 5
> x <- c(1,2,4)
> x + c(5,0,-1)
[1] 6 2 3
> x * c(5,0,-1)
[1] 5 0 -4
> x <- c(1,2,4)
> x / c(5,4,-1)
[1] 0.2 0.5 -4.0
> x %% c(5,4,-1)
[1] 1 2 0
15. Vector Indexing
One of the most important and frequently used operations in R is that of indexing
vectors.
> y <- c(1.2,3.9,0.4,0.12)
> y[c(1,3)] # extract elements 1 and 3 of y
[1] 1.2 0.4
> y[2:3]
[1] 3.9 0.4
> v <- 3:4
> y[v]
[1] 0.40 0.12
Note that duplicates are allowed.
> x <- c(4,2,17,5)
> y <- x[c(1,1,3)]
> y
[1] 4 4 17
16. > z <- c(5,12,13)
> z[-1] # exclude element 1
[1] 12 13
> z[-1:-2] # exclude elements 1 through 2
[1] 13
> z <- c(5,12,13)
> z[1:(length(z)-1)]
[1] 5 12
Or
> z[-length(z)]
[1] 5 12
17. It produces a vector consisting of a range of numbers.
> 5:8
[1] 5 6 7 8
> 5:1
[1] 5 4 3 2 1
> i <- 2
> 1:i-1 # this means (1:i) - 1, not 1:(i-1)
[1] 0 1
> 1:(i-1)
[1] 1
Generating Useful Vectors with the : Operator
18. A generalization of : is the seq() (or sequence) function, which generates a sequence in
arithmetic progression.
> seq(from=12,to=30,by=3)
[1] 12 15 18 21 24 27 30
> x <- c(5,12,13)
> x
[1] 5 12 13
> seq(x)
[1] 1 2 3
> x <- NULL
> x
NULL
> seq(x)
integer(0)
Generating Vector Sequences with seq()
19. The rep() (or repeat) function allows us to conveniently put the same con-
stant into long vectors. The call form is,
rep(x,times)
> x <- rep(8,4)
> x
[1] 8 8 8 8
> rep(c(5,12,13),3)
[1] 5 12 13 5 12 13 5 12 13
> rep(1:3,2)
[1] 1 2 3 1 2 3
> rep(c(5,12,13),each=2)
[1] 5 5 12 12 13 13
Repeating Vector Constants with rep()
21. Table of contents
Generating Filtering
Indices
01 02
Filtering with the subset()
Function
03
The Selection Function
which()
22. Generating Filtering Indices
Another feature reflecting the functional language nature of R is filtering.
This allows us to extract a vector’s elements that satisfy certain
conditions.
> z <- c(5,2,-3,8)
> z
[1] 5 2 -3 8
> z*z>8
[1] TRUE FALSE TRUE TRUE
> z <- c(5,2,-3,8)
> y <- c(1,2,30,5)
> y[z*z > 8]
[1] 1 30 5
> x <- c(1,3,8,2,20)
> x[x > 3] <- 0
> x
[1] 1 3 0 2 0
23. Filtering with the subset() Function
Filtering can also be done with the subset() function. When applied to vectors, the
difference between using this function and ordinary filtering lies in the manner in
which NA values are handled.
> x <- c(6,1:3,NA,12)
> x
[1] 6 1 2 3 NA 12
> x[x > 5]
[1] 6 NA 12
> subset(x,x > 5)
[1] 6 12
24. The Selection Function which()
> z <- c(5,2,-3,8)
> which(z*z > 8)
[1] 1 3 4
To find the positions within z at which the condition occurs.