1/15
3rd lesson in 1 basics of R
What happens if I code > 1:20
It will give you all the integers in between 1 and 20 in increments of 1 e.g. [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
What happens if I code > 15:1
It counted backwards in increments of 1 from 15 to 1 e.g. [1] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
what do i do if i want to ask about the : colon function using R’s function ?
?`:`
use backticks in between the colon
how is another way that I can list numbers from 1 to 20 without using the : colon function?
use the seq() function e.g. seq(1,20)
let's say that instead we want a vector of numbers ranging from 0 to 10, incremented by 0.5.
seq(0, 10, by=0.5)
we don't care what the increment is and we just want a sequence of 30 numbers between 5 and 10.
seq(5, 10, length=30)
I want to find the length of my sequence, which I have stored in a variable called my_seq, how do i do that?
length(my_seq)
I want to generate a sequence from 1 to however long the sequence is in the variable my_seq so, from 1 to N so for example if my_seq has a sequence of 5, i want to generate a sequence 1 through 5, but i dont know how long the sequence is in my_seq
seq_along(my_seq) (result: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30)
I am interested in creating a vector that contains 40 zeros, how do i do this? HINT:REPEAT
rep(0, times=40) (result = [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
instead we want our vector to contain 10 repetitions of the vector (0, 1, 2)
rep(c(0,1,2), times=10) *NOTE: Dont forget the c() it tells R that 0,1,2 is a vector
let's say that rather than repeating the vector (0, 1, 2) over and over again, we want our vector to contain 10 zeros, then 10 ones, then 10 twos
rep(c(0, 1, 2), each=10) (RESULT: [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2)