Vectors

Vectors allow us to store more values inside one. Vactors can be ow only one type. It might seem useless but will be awesome when we get to looping, data.frames simlulations, randomising etc.


In [20]:
str1 = "I"
str2 = "am"
str3 = "great"
num1 = 0
num2 = -5
num3 = pi

string_vec = c(str1, str2, str3)

numeric_vec = c(num1, num2, num3)

Vectors can be easilly combined


In [21]:
combined_vec = c(numeric_vec, numeric_vec)
print(combined_vec)


[1]  0.000000 -5.000000  3.141593  0.000000 -5.000000  3.141593

Remember hoow vectors can only store one TYPE of variabe? So what would happen if we do this?


In [22]:
combined_vec = c(str1, num2)

Well? No ERROR? let's have a look at it


In [23]:
combined_vec
class(combined_vec)


  1. 'I'
  2. '-5'
'character'

Vector creation

There are many ways how to create vectors. Below are some of the most common one.

Extremely common way to create integer vectors is the following.


In [24]:
numeric_vec2 = 1:5
numeric_vec2
numeric_vec3 = -5:-10
numeric_vec3


  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  1. -5
  2. -6
  3. -7
  4. -8
  5. -9
  6. -10

We can create simple vectors with the rep function


In [25]:
all_ones = rep(1, 100)

We can also create sequences with the use of seq function.


In [26]:
small_increments = seq(1, 100, by = 0.1)
larger_increments = seq(2, 100, length.out = 50)

If you want to just increment by one, use the column operator.

Another way to create vectors is through logical comparison.


In [27]:
1:5 == seq(1,5)


  1. TRUE
  2. TRUE
  3. TRUE
  4. TRUE
  5. TRUE

In [28]:
1:10 > 7


  1. FALSE
  2. FALSE
  3. FALSE
  4. FALSE
  5. FALSE
  6. FALSE
  7. FALSE
  8. TRUE
  9. TRUE
  10. TRUE

We can also do something more fancier, such as random vectors. Will come in handy for sure


In [29]:
set.seed(666)
uniform_deviations = runif(25)
uniform_deviations


  1. 0.774368490325287
  2. 0.197224191389978
  3. 0.978013844229281
  4. 0.201327350223437
  5. 0.36124442727305
  6. 0.742611942114308
  7. 0.97872843942605
  8. 0.498113709036261
  9. 0.0133158357348293
  10. 0.259946128586307
  11. 0.775893080746755
  12. 0.0163790525402874
  13. 0.0957447842229158
  14. 0.142163540003821
  15. 0.21112623764202
  16. 0.811256443616003
  17. 0.0365471958648413
  18. 0.891637413064018
  19. 0.483236411120743
  20. 0.46666452800855
  21. 0.984224080340937
  22. 0.601345547009259
  23. 0.038344347383827
  24. 0.141495691146702
  25. 0.806385525269434

In [30]:
rand1 = sample(c(1:25), 25, rep=F) #cant't repeat
rand1


  1. 7
  2. 2
  3. 15
  4. 13
  5. 18
  6. 10
  7. 8
  8. 23
  9. 9
  10. 11
  11. 19
  12. 4
  13. 12
  14. 20
  15. 25
  16. 3
  17. 5
  18. 1
  19. 16
  20. 6
  21. 14
  22. 24
  23. 17
  24. 22
  25. 21

In [31]:
rand2 = sample(c(1:25), 25, rep=T) #can repeat
rand2


  1. 2
  2. 3
  3. 4
  4. 19
  5. 1
  6. 18
  7. 4
  8. 23
  9. 3
  10. 1
  11. 20
  12. 21
  13. 7
  14. 17
  15. 24
  16. 7
  17. 7
  18. 23
  19. 15
  20. 23
  21. 16
  22. 9
  23. 17
  24. 22
  25. 2

In [32]:
favourite_games = sample(c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko"))
favourite_games


  1. 'Pandemic'
  2. 'Cosmic Encounter'
  3. 'CS Files'
  4. 'Terra Mystica'
  5. 'Takenoko'

Vector access

vectors (and matrices) are accessed witht the [] sign - [row, column]/[i, j]. First position is the nth element in vector or nth row in matrix, second number is


In [33]:
num_vec = 1:5
games_vec = sample(c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko"))

num_vec[1] * num_vec[2]
games_vec[2]
games_vec[c(1:3)]


2
'Takenoko'
  1. 'Pandemic'
  2. 'Takenoko'
  3. 'CS Files'

In [34]:
num_vec = 1:5
games_vec = c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko")
games_vec[which(num_vec > 4)]


'Takenoko'

Another very common way to access vectors is through logical vectors.;


In [35]:
logical_vec = rep(c(T, F), 5)
logical_vec
num_vec = 1:10
num_vec
num_vec[logical_vec]


  1. TRUE
  2. FALSE
  3. TRUE
  4. FALSE
  5. TRUE
  6. FALSE
  7. TRUE
  8. FALSE
  9. TRUE
  10. FALSE
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  1. 1
  2. 3
  3. 5
  4. 7
  5. 9

We can also exclude elements from vectors by using - sign


In [36]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
game_of_dead_people[-1]
game_of_dead_people[-c(1:3, 7)]


  1. 'Sansa'
  2. 'Hodor'
  3. 'Arya'
  4. 'Ned'
  5. 'Bran'
  6. 'Rickon'
  1. 'Arya'
  2. 'Ned'
  3. 'Bran'

Vector subsetting

we can easily subset vectors by numbers, but what if we want to remove Bran from it but don't know which element it is?

We can easily use which function


In [37]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
bran_idx = which(game_of_dead_people == "Bran")
bran_idx
game_of_dead_people[bran_idx]


6
'Bran'

In [38]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
non_bran_idx = which(game_of_dead_people != "Bran")
non_bran_idx
game_of_dead_people[non_bran_idx]


  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 7
  1. 'Jon'
  2. 'Sansa'
  3. 'Hodor'
  4. 'Arya'
  5. 'Ned'
  6. 'Rickon'

In [39]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
cool_dudes = c("Jon", "Arya", "Ned")
cool_idx = which(game_of_dead_people %in% cool_dudes)
uncool_idx = which(!(game_of_dead_people %in% cool_dudes))
game_of_dead_people[cool_idx]
game_of_dead_people[uncool_idx]


  1. 'Jon'
  2. 'Arya'
  3. 'Ned'
  1. 'Sansa'
  2. 'Hodor'
  3. 'Bran'
  4. 'Rickon'

And the other, more common way, is to use logical vectors. It works very similary, just drops the function which.


In [40]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
bran_idx = game_of_dead_people == "Bran"
bran_idx
game_of_dead_people[bran_idx]


  1. FALSE
  2. FALSE
  3. FALSE
  4. FALSE
  5. FALSE
  6. TRUE
  7. FALSE
'Bran'

In [41]:
game_of_dead_people = c("Jon", "Sansa", "Hodor", "Arya", "Ned", "Bran", "Rickon")
cool_dudes = c("Jon", "Arya", "Ned")
cool_idx = game_of_dead_people %in% cool_dudes
uncool_idx = !(game_of_dead_people %in% cool_dudes)
cool_idx
uncool_idx
game_of_dead_people[cool_idx]
game_of_dead_people[uncool_idx]


  1. TRUE
  2. FALSE
  3. FALSE
  4. TRUE
  5. TRUE
  6. FALSE
  7. FALSE
  1. FALSE
  2. TRUE
  3. TRUE
  4. FALSE
  5. FALSE
  6. TRUE
  7. TRUE
  1. 'Jon'
  2. 'Arya'
  3. 'Ned'
  1. 'Sansa'
  2. 'Hodor'
  3. 'Bran'
  4. 'Rickon'

Vector math

You can do easy vector math just like in real life.


In [42]:
num_vec1 = 1:5
num_vec2 = -1:-5
num_vec2 + num_vec1


  1. 0
  2. 0
  3. 0
  4. 0
  5. 0

In [44]:
years = c(1942,1968,1980,1989:1993, 2000:2005)
years_since_1900 = years - 1900
years_since_1900


  1. 42
  2. 68
  3. 80
  4. 89
  5. 90
  6. 91
  7. 92
  8. 93
  9. 100
  10. 101
  11. 102
  12. 103
  13. 104
  14. 105

In [45]:
cm = c(5, 10, 8, 13, 1, 48, 12.5)
inches = cm * 0.393701
inches


  1. 1.968505
  2. 3.93701
  3. 3.149608
  4. 5.118113
  5. 0.393701
  6. 18.897648
  7. 4.9212625

Try what happens when you try to do math with unequal length of vectors?

Practice time

Try to solve following problems

  1. Create a vector of our names and then select one "winner" at random.
  2. Create a vector of numbers from 1 to 99 by two (1 3 5 .. 99) and then select three elements at random.
  3. Create a random logical vector of length 50 (T F T F F F T F etc.)
  4. Create a random vector of "Male" "Female" elements of lenght 50
  5. Use set.seed function to create vectors 3 and 4 and use them to select only one gender.