Skip to Content
The R Book
book

The R Book

by Michael J. Crawley
June 2007
Beginner to intermediate
950 pages
27h 8m
English
Wiley
Content preview from The R Book

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

The R Book, 2nd Edition

The R Book, 2nd Edition

Michael J. Crawley
The R Book, 3rd Edition

The R Book, 3rd Edition

Elinor Jones, Simon Harden, Michael J. Crawley
The Book of R

The Book of R

Tilman M. Davies

Publisher Resources

ISBN: 9780470510247Purchase book