<- c(11, 9, 8, 10, 5)
age c(2, 3, 5)] age[
[1] 9 8 5
-c(1, 3)] age[
[1] 9 10 5
>= 8 & age <= 10] age[age
[1] 9 8 10
(AST230) R for Data Science
R’s subsetting operators are fast and powerful and mastering them allows you concisely perform complex operations
Subsetting in R easy to learn but hard to master because you need to internalize a number of interrelated concepts
There are three subsetting operators, [[
, [
, and $
subsetting operators interact differently with different vector types (e.g. atomic vectors, lists, factors, matrices, and data frames)
Subsetting can be combined with assignment
Positional Indexing
Extract a specific variable x
from data frame
Logical Indexing
How many variables are in mtcars
? Show the list of these variables.
Extract the vector mpg
from mtcars
, and calculate its mean and standard deviation.
Check whether there is any missing value in wt
of mtcars
Obtain a data frame with mpg > 22
Obtain a data frame from mtcars
with gear=5
and cyl=4
and keep only the variables year
, model
, and cyl
list()
is the most flexible data structure of R, vectors of different lengths and/or a data frame can be included in a list
Data frame is a special case of a list and [[ ]]
is useful for extracting elements of a list
List is considered as a heterogeneous vector as its elements could be of different types
The operators [[
, [
, and $
can be used to selecting elements from a list
Create a list