Vectors in R can be created as the following:
v <- c(1, 4, 9, 16, 25)Generate Vectors
Generate consecutive integers using the : notation
1:10 # a vector from 1 to 10Generate regular sequence of numbers
seq(from = 1, to = 9, by = 2) # count up by 2Repeat an element for x times
rep(42, 5) # repeat 42 for 5 timesExtract Elements
We can get the elements of a vector with the index operator (note that R indices start from 1)
v[1] # 1We can also extract multiple elements at once by passing a vector of indices to the index operator
v[c(1, 3)] # 1 9Or we can extract a range of values using the : notation.
v[2:4] # 4 9 16Vectorized Operations
Most of the R operations are vectorized so we can operate on all the elements at once:
v * 5 - 3 # 2 17 42 77 122Filter
We can filter a vector by passing a condition to the index operator
v[v %% 2 == 0](Note that this works because v %% 2 == 0 returns a vector of TRUE and FALSE)