Pick elements out of a vector by position, by dropping, and by a condition, then average the last five closes.
Square brackets take part of a vector. closes[1] is the first element, closes[2:4] is the second to the fourth, and closes[closes > 190] is every element above 190.
In this lesson I pull single elements and ranges out of a week and a half of closing prices, show what a negative index does in R, select by a condition, and average the last five closes.
Step 1. Pick by position
R counts from 1. The first element is closes[1]. Python counts from 0.
closes <-c(185.40, 187.20, 184.90, 188.10, 190.50, 191.30, 189.70, 193.20)print(closes[1]) # -> [1] 185.4 <- the first close, not closes[0]
[1] 185.4
print(closes[3]) # -> [1] 184.9
[1] 184.9
print(length(closes)) # -> [1] 8
[1] 8
print(closes[length(closes)]) # -> [1] 193.2 <- the last close
[1] 193.2
Put a vector inside the brackets and you get several elements back. 2:4 is the vector 2, 3, 4, and both ends are included, so closes[2:4] returns three prices. Python’s closes[2:4] would return two.
print(closes[2:4]) # -> [1] 187.2 184.9 188.1
[1] 187.2 184.9 188.1
print(closes[c(1, 5, 8)]) # -> [1] 185.4 190.5 193.2 <- any positions, any order
[1] 185.4 190.5 193.2
head() and tail() take from each end without you counting.
print(head(closes, 3)) # -> [1] 185.4 187.2 184.9
[1] 185.4 187.2 184.9
print(tail(closes, 3)) # -> [1] 191.3 189.7 193.2
[1] 191.3 189.7 193.2
print(tail(closes, 1)) # -> [1] 193.2 <- the last element
[1] 193.2
tail(closes, 1) is how you take the last element in R.
Two vectors of the same length line up, so one index reads both.
The minus sign means remove, not count backwards. R has no index that counts from the end, which is why tail(closes, 1) exists.
Dropping is how you line a vector up against itself. Drop the first element and you get every close from the second day on. Drop the last and you get every close from the day before. The two have the same length, so they subtract element by element.
later <- closes[-1] # every close from day 2 onearlier <- closes[-length(closes)] # every close from day 1 to day 7print(later) # -> [1] 187.2 184.9 188.1 190.5 191.3 189.7 193.2
Take v <- c(12, 9, 15, 7, 20, 11). Print the second to the fourth elements, then everything except the first, then only the elements above 10, then the mean of the last three.