Vectors

TODO

  • link to generateData, transformData

Create vectors

Empty vectors

[1] 0 0 0 0
[1] "" "" ""
[1] FALSE FALSE FALSE FALSE FALSE

Create and combine vectors

Numeric vectors

[1] 18 20 30 24 23 21
[1] 18 20 30 24 23 21 27 21 19
[1] 18 20 30 24 23 21 17 31

Character vectors

[1] "Z" "Y" "X" "T" "S" "R"
[1] "A" "B" "C"
[1] "e" "i" "m"
[1] "ipsum" "dolor" "sit"  

Information about vectors

[1] 6
[1] 3
[1] 5 5 3

Extract and change vector elements

Extract elements with a numeric index

[1] 24
[1] 18 20 30 22 23 21

Get and change the last element

[1] 21
[1] NA

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

[1] 12

Extract elements with index vectors

Get elements

[1] 18 20 22
[1] 30 23 21
 [1] 18 18 20 20 30 30 22 22 23 23 21 21
[1] 22 NA 18

Change elements

[1] 17 30 30 25 23 21

Exclude elements

[1] 17 30 25 23 21
[1] 30 23 21
[1] 30 23 21
[1] 30 23 21

Also see help(Extract)

Types of values in vectors

[1] "word" "10"   "20"   "30"  
[1] "character"

Named elements

   elem1    elem2 
 "first" "second" 
  elem1 
"first" 
val1 val2 val3 
  10  -12   33 
[1] "val1" "val2" "val3"
  A   B   C 
 10 -12  33 

Delete elements

[1] 10 20 30
[1] 1 2 3

Vector valued comparisons

Simple comparisons

[1]  TRUE FALSE FALSE FALSE  TRUE  TRUE
[1] FALSE  TRUE FALSE
[1]  TRUE FALSE FALSE

Information about elements satisfying some condition

[1] FALSE
[1] TRUE
[1] FALSE
[1] 3
[1] 1 5 6
[1] 3

Checking for equality of vectors

[1] TRUE
[1] "Mean relative difference: 0.6"
[1] FALSE

Combine multiple logical comparisons

[1]  TRUE  TRUE  TRUE FALSE FALSE FALSE
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE

Logical index vectors

Simple and combined selection criteria

[1] 17 30 24 21
[1]  TRUE  TRUE  TRUE FALSE FALSE FALSE
[1] 17 30 30
[1] 17 30 30

The recycling rule

[1] 17 30 23
[1] 17 30 23

Convert logical index vectors to numerical ones

Problem:

[1] FALSE  TRUE FALSE    NA FALSE  TRUE
[1]  2 NA  5

Solution:

[1] 2 6
[1] 2 5
[1] FALSE  TRUE FALSE FALSE FALSE  TRUE

Get the article source from GitHub

R markdown - markdown - R code - all posts