A vector is a sequence of data elements of the same basic type: numeric, character, logical, factors, or dates (exclude: two additional vector types - complex and raw)
#Creating Vectors
Four main ways to create a vector: :, c(), seq(), rep()
# integer vector
w <- 8:17
w
## [1] 8 9 10 11 12 13 14 15 16 17
# double precision floating point (number with decimals) vector
x <- c(0.5, 0.6, 0.2)
x
## [1] 0.5 0.6 0.2
# logical vector
y <- c(TRUE, FALSE, FALSE)
y
## [1] TRUE FALSE FALSE
# Character vector
z <- c("a", "b", "c")
z
## [1] "a" "b" "c"
# generate a sequence of numbers from 1 to 21 by increments of 2
seq(from = 1, to = 21, by = 2)
## [1] 1 3 5 7 9 11 13 15 17 19 21
# generate a sequence of numbers from 1 to 21 that has 15 equal
# incremented
# numbers
seq(0, 21, length.out = 15)
## [1] 0.0 1.5 3.0 4.5 6.0 7.5 9.0 10.5 12.0 13.5 15.0 16.5
# 18.0 19.5
## [15] 21.0
# replicates the values in x a specified number of times in a
# collated fashion
rep(1:4, times = 2)
## [1] 1 2 3 4 1 2 3 4
# replicates the values in x in an uncollated fashion
rep(1:4, each = 2)
## [1] 1 1 2 2 3 3 4 4
#Coercing Vectors
When you attempt to combine different types of elements (i.e. character and numeric) they will be coerced to the most flexible type possible:
# numerics are turned to characters
str(c("a", "b", "c", 1, 2, 3))
## chr [1:6] "a" "b" "c" "1" "2" "3"
# logical are turned to numerics...
str(c(1, 2, 3, TRUE, FALSE))
## num [1:5] 1 2 3 1 0
# or character
str(c("A", "B", "C", TRUE, FALSE))
## chr [1:5] "A" "B" "C" "TRUE" "FALSE"
# assigning names to a pre-existing vector
names(v1) <- letters[1:length(v1)]
v1
## a b c d e f g h i j
## 8 9 10 11 12 13 14 15 16 17
attributes(v1)
## $names
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
# adding names when creating vectors
v2 <- c(name1 = 1, name2 = 2, name3 = 3)
v2
## name1 name2 name3
## 1 2 3
attributes(v2)
## $names
## [1] "name1" "name2" "name3"
comment(v1) <- "This is a comment on a vector"
v1
## a b c d e f g h i j
## 8 9 10 11 12 13 14 15 16 17
attributes(v1)
## $names
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
##
## $comment
## [1] "This is a comment on a vector"
v1
## a b c d e f g h i j
## 8 9 10 11 12 13 14 15 16 17
v1[2]
## b
## 9
v1[2:4]
## b c d
## 9 10 11
v1[c(2, 4, 6, 8)]
## b d f h
## 9 11 13 15
# note that you can duplicate index positions
v1[c(2, 2, 4)]
## b b d
## 9 9 11
Subsetting with negative integers:
v1[-1]
## b c d e f g h i j
## 9 10 11 12 13 14 15 16 17
v1[-c(2, 4, 6, 8)]
## a c e g i j
## 8 10 12 14 16 17
Subsetting with logical values:
v1[c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)]
## a c e f g j
## 8 10 12 13 14 17
v1[v1 < 12]
## a b c d
## 8 9 10 11
v1[v1 < 12 | v1 > 15]
## a b c d i j
## 8 9 10 11 16 17
# if logical vector is shorter than the length of the vector being
# subsetted, it will be recycled to be the same length
v1[c(TRUE, FALSE)]
## a c e g i
## 8 10 12 14 16
Subsetting with names:
v1["b"]
## b
## 9
v1[c("a", "c", "h")]
## a c h
## 8 10 15
Simplifying vs. Preserving:
Simplifying subsets returns the simplest possible data structure that can represent the output. Preserving subsets keeps the structure of the output the same as the input.
v1[1]
## a
## 8
v1[[1]]
## [1] 8
#Exercises
Check out the built-in character vector state.name.
How many elements are in this vector?
What attributes does this vector have?
Can you name each vector element with “V1”, “V2”, …, “V50” (shortcut: paste0("v", 1:50))?
Now what attributes does this vector have?
Subset state.name for those elements with the following names: V35, V17, V14, V38.