4 Subsetting
Attaching the needed libraries:
4.1 Selecting multiple elements (Exercises 4.2.6)
Q1. Fix each of the following common data frame subsetting errors:
# styler: off
mtcars[mtcars$cyl = 4, ]
mtcars[-1:4, ]
mtcars[mtcars$cyl <= 5]
mtcars[mtcars$cyl == 4 | 6, ]
# styler: on
A1. Fixed versions of these commands:
# `==` instead of `=`
mtcars[mtcars$cyl == 4, ]
# `-(1:4)` instead of `-1:4`
mtcars[-(1:4), ]
# `,` was missing
mtcars[mtcars$cyl <= 5, ]
# correct subsetting syntax
mtcars[mtcars$cyl == 4 | mtcars$cyl == 6, ]
mtcars[mtcars$cyl %in% c(4, 6), ]
Q2. Why does the following code yield five missing values?
x <- 1:5
x[NA]
#> [1] NA NA NA NA NA
A2. This is because of two reasons:
- The default type of
NA
in R is oflogical
type.
typeof(NA)
#> [1] "logical"
- R recycles indexes to match the length of the vector.
x <- 1:5
x[c(TRUE, FALSE)] # recycled to c(TRUE, FALSE, TRUE, FALSE, TRUE)
#> [1] 1 3 5
Q3. What does upper.tri()
return? How does subsetting a matrix with it work? Do we need any additional subsetting rules to describe its behaviour?
A3. The documentation for upper.tri()
states-
Returns a matrix of logicals the same size of a given matrix with entries
TRUE
in the upper triangle
(x <- outer(1:5, 1:5, FUN = "*"))
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 2 3 4 5
#> [2,] 2 4 6 8 10
#> [3,] 3 6 9 12 15
#> [4,] 4 8 12 16 20
#> [5,] 5 10 15 20 25
upper.tri(x)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] FALSE TRUE TRUE TRUE TRUE
#> [2,] FALSE FALSE TRUE TRUE TRUE
#> [3,] FALSE FALSE FALSE TRUE TRUE
#> [4,] FALSE FALSE FALSE FALSE TRUE
#> [5,] FALSE FALSE FALSE FALSE FALSE
When used with a matrix for subsetting, elements corresponding to TRUE
in the subsetting matrix are selected. But, instead of a matrix, this returns a vector:
x[upper.tri(x)]
#> [1] 2 3 6 4 8 12 5 10 15 20
Q4. Why does mtcars[1:20]
return an error? How does it differ from the similar mtcars[1:20, ]
?
A4. When indexed like a list, data frame columns at given indices will be selected.
head(mtcars[1:2])
#> mpg cyl
#> Mazda RX4 21.0 6
#> Mazda RX4 Wag 21.0 6
#> Datsun 710 22.8 4
#> Hornet 4 Drive 21.4 6
#> Hornet Sportabout 18.7 8
#> Valiant 18.1 6
mtcars[1:20]
doesn’t work because there are only 11 columns in mtcars
dataset.
On the other hand, mtcars[1:20, ]
indexes a dataframe like a matrix, and because there are indeed 20 rows in mtcars
, all columns with these rows are selected.
nrow(mtcars[1:20, ])
#> [1] 20
Q5. Implement your own function that extracts the diagonal entries from a matrix (it should behave like diag(x)
where x
is a matrix).
A5. We can combine the existing functions to our advantage:
Q6. What does df[is.na(df)] <- 0
do? How does it work?
A6. This expression replaces every instance of NA
in df
with 0
.
is.na(df)
produces a matrix of logical values, which provides a way of subsetting.
4.2 Selecting a single element (Exercises 4.3.5)
Q1. Brainstorm as many ways as possible to extract the third value from the cyl
variable in the mtcars
dataset.
A1. Possible ways to to extract the third value from the cyl
variable in the mtcars
dataset:
mtcars[["cyl"]][[3]]
#> [1] 4
mtcars[[c(2, 3)]]
#> [1] 4
mtcars[3, ][["cyl"]]
#> [1] 4
mtcars[3, ]$cyl
#> [1] 4
mtcars[3, "cyl"]
#> [1] 4
mtcars[, "cyl"][[3]]
#> [1] 4
mtcars[3, 2]
#> [1] 4
mtcars$cyl[[3]]
#> [1] 4
Q2. Given a linear model, e.g., mod <- lm(mpg ~ wt, data = mtcars)
, extract the residual degrees of freedom. Then extract the R squared from the model summary (summary(mod)
)
A2. Given that objects of class lm
are lists, we can use subsetting operators to extract elements we want.
- extracting the residual degrees of freedom
mod$df.residual
#> [1] 30
mod[["df.residual"]]
#> [1] 30
- extracting the R squared from the model summary
4.3 Applications (Exercises 4.5.9)
Q1. How would you randomly permute the columns of a data frame? (This is an important technique in random forests.) Can you simultaneously permute the rows and columns in one step?
A1. Let’s create a small data frame to work with.
df <- head(mtcars)
# original
df
#> mpg cyl disp hp drat wt qsec vs am
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0
#> gear carb
#> Mazda RX4 4 4
#> Mazda RX4 Wag 4 4
#> Datsun 710 4 1
#> Hornet 4 Drive 3 1
#> Hornet Sportabout 3 2
#> Valiant 3 1
To randomly permute the columns of a data frame, we can combine [
and sample()
as follows:
- randomly permute columns
df[sample.int(ncol(df))]
#> drat wt carb am qsec vs hp mpg disp
#> Mazda RX4 3.90 2.620 4 1 16.46 0 110 21.0 160
#> Mazda RX4 Wag 3.90 2.875 4 1 17.02 0 110 21.0 160
#> Datsun 710 3.85 2.320 1 1 18.61 1 93 22.8 108
#> Hornet 4 Drive 3.08 3.215 1 0 19.44 1 110 21.4 258
#> Hornet Sportabout 3.15 3.440 2 0 17.02 0 175 18.7 360
#> Valiant 2.76 3.460 1 0 20.22 1 105 18.1 225
#> cyl gear
#> Mazda RX4 6 4
#> Mazda RX4 Wag 6 4
#> Datsun 710 4 4
#> Hornet 4 Drive 6 3
#> Hornet Sportabout 8 3
#> Valiant 6 3
- randomly permute rows
df[sample.int(nrow(df)), ]
#> mpg cyl disp hp drat wt qsec vs am
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0
#> gear carb
#> Datsun 710 4 1
#> Mazda RX4 Wag 4 4
#> Mazda RX4 4 4
#> Hornet Sportabout 3 2
#> Hornet 4 Drive 3 1
#> Valiant 3 1
- randomly permute columns and rows
df[sample.int(nrow(df)), sample.int(ncol(df))]
#> qsec vs gear am wt drat carb disp hp
#> Mazda RX4 16.46 0 4 1 2.620 3.90 4 160 110
#> Hornet 4 Drive 19.44 1 3 0 3.215 3.08 1 258 110
#> Datsun 710 18.61 1 4 1 2.320 3.85 1 108 93
#> Mazda RX4 Wag 17.02 0 4 1 2.875 3.90 4 160 110
#> Valiant 20.22 1 3 0 3.460 2.76 1 225 105
#> Hornet Sportabout 17.02 0 3 0 3.440 3.15 2 360 175
#> mpg cyl
#> Mazda RX4 21.0 6
#> Hornet 4 Drive 21.4 6
#> Datsun 710 22.8 4
#> Mazda RX4 Wag 21.0 6
#> Valiant 18.1 6
#> Hornet Sportabout 18.7 8
Q2. How would you select a random sample of m
rows from a data frame? What if the sample had to be contiguous (i.e., with an initial row, a final row, and every row in between)?
A2. Let’s create a small data frame to work with.
df <- head(mtcars)
# original
df
#> mpg cyl disp hp drat wt qsec vs am
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0
#> gear carb
#> Mazda RX4 4 4
#> Mazda RX4 Wag 4 4
#> Datsun 710 4 1
#> Hornet 4 Drive 3 1
#> Hornet Sportabout 3 2
#> Valiant 3 1
# number of rows to sample
m <- 2L
To select a random sample of m
rows from a data frame, we can combine [
and sample()
as follows:
- random and non-contiguous sample of
m
rows from a data frame
df[sample(nrow(df), m), ]
#> mpg cyl disp hp drat wt qsec vs am gear
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4
#> carb
#> Valiant 1
#> Mazda RX4 Wag 4
- random and contiguous sample of
m
rows from a data frame
# select a random starting position from available number of rows
start_row <- sample(nrow(df) - m + 1, size = 1)
# adjust ending position while avoiding off-by-one error
end_row <- start_row + m - 1
df[start_row:end_row, ]
#> mpg cyl disp hp drat wt qsec vs am gear
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4
#> carb
#> Mazda RX4 4
#> Mazda RX4 Wag 4
Q3. How could you put the columns in a data frame in alphabetical order?
A3. we can sort columns in a data frame in the alphabetical order using [
with order()
:
4.4 Session information
sessioninfo::session_info(include_base = TRUE)
#> ─ Session info ───────────────────────────────────────────
#> setting value
#> version R version 4.4.2 (2024-10-31)
#> os Ubuntu 22.04.5 LTS
#> system x86_64, linux-gnu
#> ui X11
#> language (EN)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-12-29
#> pandoc 3.6.1 @ /opt/hostedtoolcache/pandoc/3.6.1/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
#> package * version date (UTC) lib source
#> base * 4.4.2 2024-10-31 [3] local
#> bookdown 0.41 2024-10-16 [1] RSPM
#> bslib 0.8.0 2024-07-29 [1] RSPM
#> cachem 1.1.0 2024-05-16 [1] RSPM
#> cli 3.6.3 2024-06-21 [1] RSPM
#> compiler 4.4.2 2024-10-31 [3] local
#> datasets * 4.4.2 2024-10-31 [3] local
#> digest 0.6.37 2024-08-19 [1] RSPM
#> downlit 0.4.4 2024-06-10 [1] RSPM
#> emoji 16.0.0 2024-10-28 [1] RSPM
#> evaluate 1.0.1 2024-10-10 [1] RSPM
#> fastmap 1.2.0 2024-05-15 [1] RSPM
#> fs 1.6.5 2024-10-30 [1] RSPM
#> glue 1.8.0 2024-09-30 [1] RSPM
#> graphics * 4.4.2 2024-10-31 [3] local
#> grDevices * 4.4.2 2024-10-31 [3] local
#> htmltools 0.5.8.1 2024-04-04 [1] RSPM
#> jquerylib 0.1.4 2021-04-26 [1] RSPM
#> jsonlite 1.8.9 2024-09-20 [1] RSPM
#> knitr 1.49 2024-11-08 [1] RSPM
#> lifecycle 1.0.4 2023-11-07 [1] RSPM
#> magrittr * 2.0.3 2022-03-30 [1] RSPM
#> memoise 2.0.1 2021-11-26 [1] RSPM
#> methods * 4.4.2 2024-10-31 [3] local
#> pillar 1.10.0 2024-12-17 [1] RSPM
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.29 2024-11-04 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> sessioninfo 1.2.2 2021-12-06 [1] RSPM
#> stats * 4.4.2 2024-10-31 [3] local
#> stringi 1.8.4 2024-05-06 [1] RSPM
#> stringr 1.5.1 2023-11-14 [1] RSPM
#> tibble * 3.2.1 2023-03-20 [1] RSPM
#> tools 4.4.2 2024-10-31 [3] local
#> utils * 4.4.2 2024-10-31 [3] local
#> vctrs 0.6.5 2023-12-01 [1] RSPM
#> withr 3.0.2 2024-10-28 [1] RSPM
#> xfun 0.49 2024-10-31 [1] RSPM
#> xml2 1.3.6 2023-12-04 [1] RSPM
#> yaml 2.3.10 2024-07-26 [1] RSPM
#>
#> [1] /home/runner/work/_temp/Library
#> [2] /opt/R/4.4.2/lib/R/site-library
#> [3] /opt/R/4.4.2/lib/R/library
#>
#> ──────────────────────────────────────────────────────────