Trimming Vectors Using Negative Subscripts
Individual subscripts are referred to in square brackets. So if x is like this:
x<- c(5,8,6,7,1,5,3)
we can find the 4th element of the vector just by typing
x[4]
[1] 7
An extremely useful facility is to use negative subscripts to drop terms from a vector. Suppose we wanted a new vector, z, to contain everything but the first element of x
z<- x[-1]
z
[1] 8 6 7 1 5 3
Suppose our task is to calculate a trimmed mean of x which ignores both the smallest and largest values (i.e. we want to leave out the 1 and the 8 in this example). There are two steps to this. First, we sort the vector x. Then we remove the first element using x[-1] and the last using x[-length(x)]. We can do both drops at the same time by concatenating both instructions like this: -c(1,length(x)). Then we use the built-in function mean:
trim.mean <- function (x) mean(sort(x)[-c(1,length(x))])
Now try it out. The answer should be mean(c(5,6,7,5,3)) = 26/5= 5.2:
trim.mean(x)
[1] 5.2
Suppose now that we need to produce a vector containing the numbers 1 to 50 but omitting all the multiples of seven (7, 14, 21, etc.). First make a vector of all the numbers 1 to 50 including the multiples of 7:
vec<-1:50
Now work out how many numbers there are between 1 and 50 that are multiples of 7
(multiples<-floor(50/7))
[1] 7
Now create a vector of the first seven multiples of 7 called subscripts:
(subscripts<-7*(1:multiples))
[1] 7 14 21 28 35 42 49
Finally, use negative subscripts ...
Get The R Book now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.