Vectors

TODO

  • link to generateData, transformData

Create vectors

Empty vectors

numeric(4)
[1] 0 0 0 0
character(3)
[1] "" "" ""
logical(5)
[1] FALSE FALSE FALSE FALSE FALSE

Create and combine vectors

Numeric vectors

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

Character vectors

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

Information about vectors

length(age)
[1] 6
length(chars)
[1] 3
nchar(chars)
[1] 5 5 3

Extract and change vector elements

Extract elements with a numeric index

age[4]
[1] 24
age[4] <- 22
age
[1] 18 20 30 22 23 21

Get and change the last element

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

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

c(11, 12, 13, 14)[2]
[1] 12

Extract elements with index vectors

Get elements

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

Change elements

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

Exclude elements

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

Also see help(Extract)

Types of values in vectors

charVec4 <- "word"
numVec   <- c(10, 20, 30)
(combVec <- c(charVec4, numVec))
[1] "word" "10"   "20"   "30"  
mode(combVec)
[1] "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)
[1] "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
[1] 10 20 30
vec <- c(1, 2, 3, 4, 5)
length(vec) <- 3
vec
[1] 1 2 3

Vector valued comparisons

Simple comparisons

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

Information about elements satisfying some condition

res <- age > 30
any(res)
[1] FALSE
any(age < 18)
[1] TRUE
all(x == y)
[1] FALSE
res <- age < 24
sum(res)
[1] 3
which(age < 24)
[1] 1 5 6
length(which(age < 24))
[1] 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)
[1] TRUE
all.equal(y, z)
[1] "Mean relative difference: 0.6"
isTRUE(all.equal(y, z))
[1] FALSE

Combine multiple logical comparisons

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

Logical index vectors

Simple and combined selection criteria

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

The recycling rule

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

Convert logical index vectors to numerical ones

Problem:

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

Solution:

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

Get the article source from GitHub

R markdown - markdown - R code - all posts