Subsetting R Object
Presented By:
Dr. Poonam Panwar
Associate Professor
MMICT & BM
MM(DU), Mullana, Ambala, Haryana
Subsetting R Object
There are three operators that can be used to extract subsets of R
objects.
• The [ operator always returns an object of the same class as the
original. It can be used to select multiple elements of an object
• The [[ operator is used to extract elements of a list or a data frame. It
can only be used to extract a single element and the class of the
returned object will not necessarily be a list or data frame.
• The $ operator is used to extract elements of a list or data frame by
literal name. Its semantics are similar to that of [[.
29/08/2025 By: Dr. Poonam Panwar 2
Subsetting a Vector
Vectors are basic objects in R and they can be subsetted using the
[ operator.
29/08/2025 By: Dr. Poonam Panwar 3
Subsetting a Vector
The [ operator can be used to extract multiple elements of a vector by
passing the operator an integer sequence. Here we extract the first four
elements of the vector.
The sequence does not have to be in order; you can specify any arbitrary
integer vector.
29/08/2025 By: Dr. Poonam Panwar 4
Subsetting a Vector
We can also pass a logical sequence to the [ operator to extract
elements of a vector that satisfy a given condition. For example, here
we want the elements of x that come lexicographically after the letter
“a”.
29/08/2025 By: Dr. Poonam Panwar 5
Subsetting a Vector
Another, more compact, way to do this would be to skip the creation of
a logical vector and just subset the vector directly with the logical
expression.
29/08/2025 By: Dr. Poonam Panwar 6
Subsetting a Matrix
Matrices can be subsetted in the usual way with (i,j) type indices. Here,
we create simple 2×3 matrix with the matrix function.
29/08/2025 By: Dr. Poonam Panwar 7
Subsetting a Matrix
We can access the (1, 2) or the (2, 1) element of this matrix using the
appropriate indices.
29/08/2025 By: Dr. Poonam Panwar 8
Subsetting a Matrix
Indices can also be missing. This behavior is used to access entire rows
or columns of a matrix.
29/08/2025 By: Dr. Poonam Panwar 9
Dropping matrix dimensions
By default, when a single element of a matrix is retrieved, it is returned
as a vector of length 1 rather than a 1×1 matrix. Often, this is exactly
what we want, but this behavior can be turned off by setting drop =
FALSE.
29/08/2025 By: Dr. Poonam Panwar 10
Dropping matrix dimensions
Similarly, when we extract a single row or column of a matrix, R by
default drops the dimension of length 1, so instead of getting a 1 × 3
matrix after extracting the first row, we get a vector of length 3. This
behavior can similarly be turned off with the drop = FALSE option.
Be careful of R’s automatic dropping of dimensions. This is a feature
that is often quite useful during interactive work, but can later come
back to bite you when you are writing longer programs or functions.
29/08/2025 By: Dr. Poonam Panwar 11
Subsetting Lists
Lists in R can be subsetted using all three of the operators mentioned
above, and all three are used for different purposes.
29/08/2025 By: Dr. Poonam Panwar 12
Subsetting Lists
The [[ operator can be used to extract single elements from a list. Here
we extract the first element of the list.
The [[ operator can also use named indices so that you don’t have to
remember the exact ordering of every element of the list. You can also
use the $ operator to extract elements by name. Notice you don’t need
the quotes when you use the $ operator.
29/08/2025 By: Dr. Poonam Panwar 13
Subsetting Lists
One thing that differentiates the [[ operator from the $ is that the
[[ operator can be used with computed indices. The $ operator can only
be used with literal names.
29/08/2025 By: Dr. Poonam Panwar 14
Subsetting Nested Elements of a List
The [[ operator can take an integer sequence if you want to extract a
nested element of a list.
29/08/2025 By: Dr. Poonam Panwar 15
Extracting Multiple Elements of a List
The [ operator can be used to extract multiple elements from a list. For
example, if you wanted to extract the first and third elements of a list,
you would do the following
Note that x[c(1, 3)] is NOT the same as x[[c(1, 3)]].
Remember that the [ operator always returns an object of the same class as
the original. Since the original object was a list, the [ operator returns a
list. In the above code, we returned a list with two elements (the first and
the third).
29/08/2025 By: Dr. Poonam Panwar 16
Partial Matching
Partial matching of names is allowed with [[ and $. This is often very
useful during interactive work if the object you’re working with has very
long element names. You can just abbreviate those names and R will
figure out what element you’re referring to.
In general, this is fine for interactive work, but you shouldn’t resort to
partial matching if you are writing longer scripts, functions, or programs.
In those cases, you should refer to the full element name if possible. That
way there’s no ambiguity in your code.
29/08/2025 By: Dr. Poonam Panwar 17
Vectorized Operations
Many operations in R are vectorized, meaning that operations occur in
parallel in certain R objects. This allows you to write code that is
efficient, concise, and easier to read than in non-vectorized languages.
The simplest example is when adding two vectors together.
29/08/2025 By: Dr. Poonam Panwar 18
Vectorized Operations
Natural, right? Without vectorization, you’d have to do something like
If you had to do that every time you wanted to add two vectors, your
hands would get very tired from all the typing.
29/08/2025 By: Dr. Poonam Panwar 19
Vectorized Operations
Another operation you can do in a vectorized manner is logical
comparisons. So suppose you wanted to know which elements of a
vector were greater than 2. You could do he following.
29/08/2025 By: Dr. Poonam Panwar 20
Vectorized Operations
Here are other vectorized logical operations.
Notice that these logical operations return a logical vector of TRUE and
FALSE.
29/08/2025 By: Dr. Poonam Panwar 21
Vectorized Operations
Of course, subtraction, multiplication and division are also vectorized.
29/08/2025 By: Dr. Poonam Panwar 22
Vectorized Matrix Operations
Matrix operations are
also vectorized, making
for nicely compact
notation. This way, we
can do element-by-
element operations on
matrices without having
to loop over every
element.
29/08/2025 By: Dr. Poonam Panwar 23
Thank You
29/08/2025 By: Dr. Poonam Panwar 24