# Vectors

## Create vectors

### Empty vectors

numeric(4)
 0 0 0 0
character(3)
 "" "" ""
logical(5)
 FALSE FALSE FALSE FALSE FALSE

### Create and combine vectors

Numeric vectors

(age <- c(18, 20, 30, 24, 23, 21))
 18 20 30 24 23 21
addAge  <- c(27, 21, 19)
(ageNew <- c(age, addAge))
 18 20 30 24 23 21 27 21 19
append(age, c(17, 31))
 18 20 30 24 23 21 17 31

Character vectors

charVec1 <- c("Z", "Y", "X")
(charVec2 <- c(charVec1, "T", "S", "R"))
 "Z" "Y" "X" "T" "S" "R"
LETTERS[c(1, 2, 3)]
 "A" "B" "C"
letters[c(5, 9, 13)]
 "e" "i" "m"
(chars <- c("ipsum", "dolor", "sit"))
 "ipsum" "dolor" "sit"  

length(age)
 6
length(chars)
 3
nchar(chars)
 5 5 3

## Extract and change vector elements

### Extract elements with a numeric index

age
 24
age <- 22
age
 18 20 30 22 23 21

Get and change the last element

(ageLast <- age[length(age)])
 21
age[length(age) + 1]
 NA

A vector does not need a name for getting one of its values

c(11, 12, 13, 14)
 12

### Extract elements with index vectors

Get elements

idx <- c(1, 2, 4)
age[idx]
 18 20 22
age[c(3, 5, 6)]
 30 23 21
age[c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6)]
  18 18 20 20 30 30 22 22 23 23 21 21
age[c(4, NA, 1)]
 22 NA 18

Change elements

age[idx] <- c(17, 30, 25)
age
 17 30 30 25 23 21

### Exclude elements

age[-3]
 17 30 25 23 21
age[c(-1, -2, -4)]
 30 23 21
age[-c(1, 2, 4)]
 30 23 21
age[-idx]
 30 23 21

Also see help(Extract)

## Types of values in vectors

charVec4 <- "word"
numVec   <- c(10, 20, 30)
(combVec <- c(charVec4, numVec))
 "word" "10"   "20"   "30"  
mode(combVec)
 "character"

## Named elements

(namedVec1 <- c(elem1="first", elem2="second"))
   elem1    elem2
"first" "second" 
namedVec1["elem1"]
  elem1
"first" 
(namedVec2 <- c(val1=10, val2=-12, val3=33))
val1 val2 val3
10  -12   33 
names(namedVec2)
 "val1" "val2" "val3"
names(namedVec2) <- c("A", "B", "C")
namedVec2
  A   B   C
10 -12  33 

## Delete elements

vec <- c(10, 20, 30, 40, 50)
vec <- vec[c(-4, -5)]
vec
 10 20 30
vec <- c(1, 2, 3, 4, 5)
length(vec) <- 3
vec
 1 2 3

## Vector valued comparisons

### Simple comparisons

age <- c(17, 30, 30, 24, 23, 21)
age < 24
  TRUE FALSE FALSE FALSE  TRUE  TRUE
x <- c(2, 4, 8)
y <- c(3, 4, 5)
x == y
 FALSE  TRUE FALSE
x < y
  TRUE FALSE FALSE

Information about elements satisfying some condition

res <- age > 30
any(res)
 FALSE
any(age < 18)
 TRUE
all(x == y)
 FALSE
res <- age < 24
sum(res)
 3
which(age < 24)
 1 5 6
length(which(age < 24))
 3

Checking for equality of vectors

x <- c(4, 5, 6)
y <- c(4, 5, 6)
z <- c(1, 2, 3)
all.equal(x, y)
 TRUE
all.equal(y, z)
 "Mean relative difference: 0.6"
isTRUE(all.equal(y, z))
 FALSE

### Combine multiple logical comparisons

(age <= 20) | (age >= 30)
  TRUE  TRUE  TRUE FALSE FALSE FALSE
(age > 20) & (age < 30)
 FALSE FALSE FALSE  TRUE  TRUE  TRUE

## Logical index vectors

### Simple and combined selection criteria

age[c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE)]
 17 30 24 21
(idx <- (age <= 20) | (age >= 30))
  TRUE  TRUE  TRUE FALSE FALSE FALSE
age[idx]
 17 30 30
age[(age >= 30) | (age <= 20)]
 17 30 30

### The recycling rule

age[c(TRUE, FALSE)]
 17 30 23
age[c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)]
 17 30 23

### Convert logical index vectors to numerical ones

Problem:

vecNA   <- c(-3, 2, 0, NA, -7, 5)
(logIdx <- vecNA > 0)
 FALSE  TRUE FALSE    NA FALSE  TRUE
vecNA[logIdx]
  2 NA  5

Solution:

(numIdx <- which(logIdx))
 2 6
vecNA[numIdx]
 2 5
seq(along=logIdx) %in% numIdx
 FALSE  TRUE FALSE FALSE FALSE  TRUE