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)
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)
Extremely common way to create integer vectors is the following.
In [24]:
numeric_vec2 = 1:5
numeric_vec2
numeric_vec3 = -5:-10
numeric_vec3
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)
In [28]:
1:10 > 7
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
In [30]:
rand1 = sample(c(1:25), 25, rep=F) #cant't repeat
rand1
In [31]:
rand2 = sample(c(1:25), 25, rep=T) #can repeat
rand2
In [32]:
favourite_games = sample(c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko"))
favourite_games
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)]
In [34]:
num_vec = 1:5
games_vec = c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko")
games_vec[which(num_vec > 4)]
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]
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)]
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]
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]
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]
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]
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]
In [42]:
num_vec1 = 1:5
num_vec2 = -1:-5
num_vec2 + num_vec1
In [44]:
years = c(1942,1968,1980,1989:1993, 2000:2005)
years_since_1900 = years - 1900
years_since_1900
In [45]:
cm = c(5, 10, 8, 13, 1, 48, 12.5)
inches = cm * 0.393701
inches
Try what happens when you try to do math with unequal length of vectors?
Try to solve following problems