<jupyter_start><jupyter_text>VectorsVectors 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.<jupyter_code>str1 = "I"
str2 = "am"
str3 = "great"
num1 = 0
num2 = -5
num3 = pi

string_vec = c(str1, str2, str3)

numeric_vec = c(num1, num2, num3)<jupyter_output><empty_output><jupyter_text>Vectors can be easilly combined<jupyter_code>combined_vec = c(numeric_vec, numeric_vec)
print(combined_vec)<jupyter_output>[1]  0.000000 -5.000000  3.141593  0.000000 -5.000000  3.141593<jupyter_text>Remember hoow vectors can only store one TYPE of variabe? So what would happen if we do this?<jupyter_code>combined_vec = c(str1, num2)<jupyter_output><empty_output><jupyter_text>Well? No ERROR? let's have a look at it<jupyter_code>combined_vec
class(combined_vec)<jupyter_output><empty_output><jupyter_text>Vector creationThere 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.<jupyter_code>numeric_vec2 = 1:5
numeric_vec2
numeric_vec3 = -5:-10
numeric_vec3<jupyter_output><empty_output><jupyter_text>We can create simple vectors with the rep function<jupyter_code>all_ones = rep(1, 100)<jupyter_output><empty_output><jupyter_text>We can also create sequences with the use of seq function.<jupyter_code>small_increments = seq(1, 100, by = 0.1)
larger_increments = seq(2, 100, length.out = 50)<jupyter_output><empty_output><jupyter_text>If you want to just increment by one, use the column operator. Another way to create vectors is through logical comparison.<jupyter_code>1:5 == seq(1,5)
1:10 > 7<jupyter_output><empty_output><jupyter_text>We can also do something more fancier, such as random vectors. Will come in handy for sure<jupyter_code>set.seed(666)
uniform_deviations = runif(25)
uniform_deviations
rand1 = sample(c(1:25), 25, rep=F) #cant't repeat
rand1
rand2 = sample(c(1:25), 25, rep=T) #can repeat
rand2
favourite_games = sample(c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko"))
favourite_games<jupyter_output><empty_output><jupyter_text>Vector accessvectors (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<jupyter_code>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)]
num_vec = 1:5
games_vec = c("CS Files", "Terra Mystica", "Cosmic Encounter", "Pandemic", "Takenoko")
games_vec[which(num_vec > 4)]<jupyter_output><empty_output><jupyter_text>Another very common way to access vectors is through logical vectors.;<jupyter_code>logical_vec = rep(c(T, F), 5)
logical_vec
num_vec = 1:10
num_vec
num_vec[logical_vec]<jupyter_output><empty_output><jupyter_text>We can also exclude elements from vectors by using - sign<jupyter_code>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)]<jupyter_output><empty_output><jupyter_text>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<jupyter_code>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]
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]
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]<jupyter_output><empty_output><jupyter_text>And the other, more common way, is to use logical vectors. It works very similary, just drops the function which.<jupyter_code>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]
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]<jupyter_output><empty_output><jupyter_text>Vector mathYou can do easy vector math just like in real life.<jupyter_code>num_vec1 = 1:5
num_vec2 = -1:-5
num_vec2 + num_vec1
years = c(1942,1968,1980,1989:1993, 2000:2005)
years_since_1900 = years - 1900
years_since_1900
cm = c(5, 10, 8, 13, 1, 48, 12.5)
inches = cm * 0.393701
inches<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1225754849030194, 'fraction_numerical': 0.039192161567686463, 'mean_word_length': 7.535836177474403, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '236059', 'license': 'MIT', 'n_tokens_mistral': 2088, 'n_tokens_neox': 1919, 'n_words': 478, 'path': 'Notebooks/Session1/Vectors.ipynb', 'repo_name': 'lab-code/R-for-non-programmers', 'size': '5001'}