These are applications of base R taken from Advanced R by Hadley Wickham.
You can make a lookup table using named vectors and character subsetting:
lookup <- c(m = "Male", f = "Female", u = NA)
x <- c("m", "f", "u", "f", "f", "m", "m")
lookup[x]
## m f u f f m m
## "Male" "Female" NA "Female" "Female" "Male" "Male"
You may have a more complicated lookup table which has multiple columns of information. Suppose we have a vector of integer grades, and a table that describes their properties:
info <- data.frame(
grade = 3:1,
desc = c("Excellent", "Good", "Poor"),
fail = c(F, F, T)
)
grades <- c(1, 2, 2, 3, 1)
We want to duplicate the info table so that we have a row for each value in grades. We can do this using match()
and integer subsetting:
# Using match
info
## grade desc fail
## 1 3 Excellent FALSE
## 2 2 Good FALSE
## 3 1 Poor TRUE
grades
## [1] 1 2 2 3 1
(id <- match(grades, info$grade))
## [1] 3 2 2 1 3
info[id, ]
## grade desc fail
## 3 1 Poor TRUE
## 2 2 Good FALSE
## 2.1 2 Good FALSE
## 1 3 Excellent FALSE
## 3.1 1 Poor TRUE
You can use integer indices to perform random sampling of a vector or data frame.
(df <- data.frame(x = rep(1:3, each = 2), y = 6:1, z = letters[1:6]))
## x y z
## 1 1 6 a
## 2 1 5 b
## 3 2 4 c
## 4 2 3 d
## 5 3 2 e
## 6 3 1 f
# Set seed for reproducibility
set.seed(10)
# Randomly reorder (permutation)
df[sample(nrow(df)), ]
## x y z
## 4 2 3 d
## 2 1 5 b
## 5 3 2 e
## 3 2 4 c
## 1 1 6 a
## 6 3 1 f
# With replacement
df[sample(nrow(df), 6, replace = TRUE), ]
## x y z
## 2 1 5 b
## 2.1 1 5 b
## 4 2 3 d
## 3 2 4 c
## 4.1 2 3 d
## 4.2 2 3 d
# Select 3 random rows
df[sample(nrow(df), 3), ]
## x y z
## 1 1 6 a
## 3 2 4 c
## 2 1 5 b
Functioon order()
returns a permutation which rearranges its first argument into ascending or descending order:
x <- c("b", "c", "a")
order(x)
## [1] 3 1 2
order(x, decreasing = TRUE)
## [1] 2 1 3
x[order(x)]
## [1] "a" "b" "c"
sort(x)
## [1] "a" "b" "c"
x[order(x, decreasing = TRUE)]
## [1] "c" "b" "a"
sort(x, decreasing = TRUE)
## [1] "c" "b" "a"
# Randomly shuffle df
df2 <- df[sample(nrow(df)), 3:1]
df2
## z y x
## 3 c 4 2
## 1 a 6 1
## 2 b 5 1
## 4 d 3 2
## 6 f 1 3
## 5 e 2 3
# reorder rows
df2[order(df2$x), ]
## z y x
## 1 a 6 1
## 2 b 5 1
## 3 c 4 2
## 4 d 3 2
## 6 f 1 3
## 5 e 2 3
# reaorder also columns
df2[order(df2$x), order(names(df2))]
## x y z
## 1 1 6 a
## 2 1 5 b
## 3 2 4 c
## 4 2 3 d
## 6 3 1 f
## 5 3 2 e
Because it allows you to easily combine conditions from multiple columns, logical subsetting is probably the most commonly used technique for extracting rows out of a data frame.
mtcars
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
mtcars[mtcars$gear == 5, ]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
mtcars[mtcars$gear == 5 & mtcars$cyl == 4, ]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
Don’t forget De Morgan’s laws:
\[\neg (X \wedge Y) \equiv \neg X \vee \neg Y \\ \neg (X \vee Y) \equiv \neg X \wedge \neg Y\]
Simplify the following: \[\neg (X \vee \neg (Y \wedge X))\]
Function subset()
is a specialised shorthand function for subsetting data frames, and saves some typing because you don’t need to repeat the name of the data frame:
subset(mtcars, gear == 5)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
subset(mtcars, gear == 5 & cyl == 4)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
It’s useful to be aware of the natural equivalence between set operations (integer subsetting) and boolean algebra (logical subsetting). Let’s create two logical vectors and their integer equivalents and then explore the relationship between boolean and set operations. We take advantace of which()
that allows you to convert a boolean representation to an integer representation.
(x1 <- 1:10 %% 2 == 0)
## [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
(y1 <- 1:10 %% 5 == 0)
## [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
(x2 <- which(x1))
## [1] 2 4 6 8 10
(y2 <- which(y1))
## [1] 5 10
# X & Y <-> intersect(x, y)
x1 & y1
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
intersect(x2, y2)
## [1] 10
# X | Y <-> union(x, y)
x1 | y1
## [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE
union(x2, y2)
## [1] 2 4 6 8 10 5
# X & !Y <-> setdiff(x, y)
x1 & !y1
## [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE
setdiff(x2, y2)
## [1] 2 4 6 8
Question: express exclusive or (xor) using setdiff and union:
xor(x1, y1)
## [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE