This vignette compares dplyr functions to their base R equivalents. This helps those familiar with base R understand better what dplyr does, and shows dplyr users how you might express the same ideas in base R code. We’ll start with a rough overview of the major differences, then discuss the one table verbs in more detail, followed by the two table verbs.
The code dplyr verbs input and output data frames. This contrasts with base R functions which more frequently work with individual vectors.
dplyr relies heavily on “non-standard evaluation” so that you
don’t need to use $
to refer to columns in the “current”
data frame. This behaviour is inspired by the base functions
subset()
and transform()
.
dplyr solutions tend to use a variety of single purpose verbs,
while base R solutions typically tend to use [
in a variety
of ways, depending on the task at hand.
Multiple dplyr verbs are often strung together into a pipeline by
%>%
. In base R, you’ll typically save intermediate
results to a variable that you either discard, or repeatedly
overwrite.
All dplyr verbs handle “grouped” data frames so that the code to perform a computation per-group looks very similar to code that works on a whole data frame. In base R, per-group operations tend to have varied forms.
The following table shows a condensed translation between dplyr verbs
and their base R equivalents. The following sections describe each
operation in more detail. You learn more about the dplyr verbs in their
documentation and in For more vignette("one-table")
.
dplyr | base |
---|---|
arrange(df, x) |
df[order(x), , drop = FALSE] |
distinct(df, x) |
df[!duplicated(x), , drop = FALSE] ,
unique() |
filter(df, x) |
df[which(x), , drop = FALSE] ,
subset() |
mutate(df, z = x + y) |
df$z <- df$x + df$y , transform() |
pull(df, 1) |
df[[1]] |
pull(df, x) |
df$x |
rename(df, y = x) |
names(df)[names(df) == "x"] <- "y" |
relocate(df, y) |
df[union("y", names(df))] |
select(df, x, y) |
df[c("x", "y")] , subset() |
select(df, starts_with("x")) |
df[grepl(names(df), "^x")] |
summarise(df, mean(x)) |
mean(df$x) , tapply() ,
aggregate() , by() |
slice(df, c(1, 2, 5)) |
df[c(1, 2, 5), , drop = FALSE] |
To begin, we’ll load dplyr and convert mtcars
and
iris
to tibbles so that we can easily show only abbreviated
output for each operation.
library(dplyr)
<- as_tibble(mtcars)
mtcars <- as_tibble(iris) iris
arrange()
: Arrange rows by variablesdplyr::arrange()
orders the rows of a data frame by the
values of one or more columns:
%>% arrange(cyl, disp)
mtcars #> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 33.9 4 71.1 65 4.22 1.84 19.9 1 1 4 1
#> 2 30.4 4 75.7 52 4.93 1.62 18.5 1 1 4 2
#> 3 32.4 4 78.7 66 4.08 2.2 19.5 1 1 4 1
#> 4 27.3 4 79 66 4.08 1.94 18.9 1 1 4 1
#> # … with 28 more rows
The desc()
helper allows you to order selected variables
in descending order:
%>% arrange(desc(cyl), desc(disp))
mtcars #> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 10.4 8 472 205 2.93 5.25 18.0 0 0 3 4
#> 2 10.4 8 460 215 3 5.42 17.8 0 0 3 4
#> 3 14.7 8 440 230 3.23 5.34 17.4 0 0 3 4
#> 4 19.2 8 400 175 3.08 3.84 17.0 0 0 3 2
#> # … with 28 more rows
We can replicate in base R by using [
with
order()
:
order(mtcars$cyl, mtcars$disp), , drop = FALSE]
mtcars[#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 33.9 4 71.1 65 4.22 1.84 19.9 1 1 4 1
#> 2 30.4 4 75.7 52 4.93 1.62 18.5 1 1 4 2
#> 3 32.4 4 78.7 66 4.08 2.2 19.5 1 1 4 1
#> 4 27.3 4 79 66 4.08 1.94 18.9 1 1 4 1
#> # … with 28 more rows
Note the use of drop = FALSE
. If you forget this, and
the input is a data frame with a single column, the output will be a
vector, not a data frame. This is a source of subtle bugs.
Base R does not provide a convenient and general way to sort individual variables in descending order, so you have two options:
-x
.order()
to sort all variables in
descending order.order(mtcars$cyl, mtcars$disp, decreasing = TRUE), , drop = FALSE]
mtcars[order(-mtcars$cyl, -mtcars$disp), , drop = FALSE] mtcars[
distinct()
: Select distinct/unique rowsdplyr::distinct()
selects unique rows:
<- tibble(
df x = sample(10, 100, rep = TRUE),
y = sample(10, 100, rep = TRUE)
)
%>% distinct(x) # selected columns
df #> # A tibble: 10 × 1
#> x
#> <int>
#> 1 1
#> 2 4
#> 3 2
#> 4 6
#> # … with 6 more rows
%>% distinct(x, .keep_all = TRUE) # whole data frame
df #> # A tibble: 10 × 2
#> x y
#> <int> <int>
#> 1 1 5
#> 2 4 1
#> 3 2 3
#> 4 6 7
#> # … with 6 more rows
There are two equivalents in base R, depending on whether you want the whole data frame, or just selected variables:
unique(df["x"]) # selected columns
#> # A tibble: 10 × 1
#> x
#> <int>
#> 1 1
#> 2 4
#> 3 2
#> 4 6
#> # … with 6 more rows
!duplicated(df$x), , drop = FALSE] # whole data frame
df[#> # A tibble: 10 × 2
#> x y
#> <int> <int>
#> 1 1 5
#> 2 4 1
#> 3 2 3
#> 4 6 7
#> # … with 6 more rows
filter()
: Return rows with matching conditionsdplyr::filter()
selects rows where an expression is
TRUE
:
%>% filter(species == "Human")
starwars #> # A tibble: 35 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
#> 2 Darth Va… 202 136 none white yellow 41.9 male mascu…
#> 3 Leia Org… 150 49 brown light brown 19 fema… femin…
#> 4 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
#> # … with 31 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
%>% filter(mass > 1000)
starwars #> # A tibble: 1 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Jabba De… 175 1358 <NA> green-tan… orange 600 herm… mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>
%>% filter(hair_color == "none" & eye_color == "black")
starwars #> # A tibble: 9 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Nien Nunb 160 68 none grey black NA male mascu…
#> 2 Gasgano 122 NA none white, bl… black NA male mascu…
#> 3 Kit Fisto 196 87 none green black NA male mascu…
#> 4 Plo Koon 188 80 none orange black 22 male mascu…
#> # … with 5 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
The closest base equivalent (and the inspiration for
filter()
) is subset()
:
subset(starwars, species == "Human")
#> # A tibble: 35 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
#> 2 Darth Va… 202 136 none white yellow 41.9 male mascu…
#> 3 Leia Org… 150 49 brown light brown 19 fema… femin…
#> 4 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
#> # … with 31 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
subset(starwars, mass > 1000)
#> # A tibble: 1 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Jabba De… 175 1358 <NA> green-tan… orange 600 herm… mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>
subset(starwars, hair_color == "none" & eye_color == "black")
#> # A tibble: 9 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Nien Nunb 160 68 none grey black NA male mascu…
#> 2 Gasgano 122 NA none white, bl… black NA male mascu…
#> 3 Kit Fisto 196 87 none green black NA male mascu…
#> 4 Plo Koon 188 80 none orange black 22 male mascu…
#> # … with 5 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
You can also use [
but this also requires the use of
which()
to remove NA
s:
which(starwars$species == "Human"), , drop = FALSE]
starwars[#> # A tibble: 35 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Luke Sky… 172 77 blond fair blue 19 male mascu…
#> 2 Darth Va… 202 136 none white yellow 41.9 male mascu…
#> 3 Leia Org… 150 49 brown light brown 19 fema… femin…
#> 4 Owen Lars 178 120 brown, gr… light blue 52 male mascu…
#> # … with 31 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
which(starwars$mass > 1000), , drop = FALSE]
starwars[#> # A tibble: 1 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Jabba De… 175 1358 <NA> green-tan… orange 600 herm… mascu…
#> # … with 5 more variables: homeworld <chr>, species <chr>, films <list>,
#> # vehicles <list>, starships <list>
which(starwars$hair_color == "none" & starwars$eye_color == "black"), , drop = FALSE]
starwars[#> # A tibble: 9 × 14
#> name height mass hair_color skin_color eye_color birth_year sex gender
#> <chr> <int> <dbl> <chr> <chr> <chr> <dbl> <chr> <chr>
#> 1 Nien Nunb 160 68 none grey black NA male mascu…
#> 2 Gasgano 122 NA none white, bl… black NA male mascu…
#> 3 Kit Fisto 196 87 none green black NA male mascu…
#> 4 Plo Koon 188 80 none orange black 22 male mascu…
#> # … with 5 more rows, and 5 more variables: homeworld <chr>, species <chr>,
#> # films <list>, vehicles <list>, starships <list>
mutate()
: Create or transform variablesdplyr::mutate()
creates new variables from existing
variables:
%>% mutate(z = x + y, z2 = z ^ 2)
df #> # A tibble: 100 × 4
#> x y z z2
#> <int> <int> <int> <dbl>
#> 1 1 5 6 36
#> 2 4 1 5 25
#> 3 2 3 5 25
#> 4 6 7 13 169
#> # … with 96 more rows
The closest base equivalent is transform()
, but note
that it cannot use freshly created variables:
head(transform(df, z = x + y, z2 = (x + y) ^ 2))
#> x y z z2
#> 1 1 5 6 36
#> 2 4 1 5 25
#> 3 2 3 5 25
#> 4 6 7 13 169
#> 5 6 5 11 121
#> 6 4 5 9 81
Alternatively, you can use $<-
:
$cyl2 <- mtcars$cyl * 2
mtcars$cyl4 <- mtcars$cyl2 * 2 mtcars
When applied to a grouped data frame, dplyr::mutate()
computes new variable once per group:
<- tibble(g = c(1, 1, 2, 2), x = c(0.5, 1.5, 2.5, 3.5))
gf %>%
gf group_by(g) %>%
mutate(x_mean = mean(x), x_rank = rank(x))
#> # A tibble: 4 × 4
#> # Groups: g [2]
#> g x x_mean x_rank
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1 0.5 1 1
#> 2 1 1.5 1 2
#> 3 2 2.5 3 1
#> 4 2 3.5 3 2
To replicate this in base R, you can use ave()
:
transform(gf,
x_mean = ave(x, g, FUN = mean),
x_rank = ave(x, g, FUN = rank)
)#> g x x_mean x_rank
#> 1 1 0.5 1 1
#> 2 1 1.5 1 2
#> 3 2 2.5 3 1
#> 4 2 3.5 3 2
pull()
: Pull out a single variabledplyr::pull()
extracts a variable either by name or
position:
%>% pull(1)
mtcars #> [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
#> [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
#> [31] 15.0 21.4
%>% pull(cyl)
mtcars #> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
This equivalent to [[
for positions and $
for names:
1]]
mtcars[[#> [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
#> [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
#> [31] 15.0 21.4
$cyl
mtcars#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
relocate()
: Change column orderdplyr::relocate()
makes it easy to move a set of columns
to a new position (by default, the front):
# to front
%>% relocate(gear, carb)
mtcars #> # A tibble: 32 × 13
#> gear carb mpg cyl disp hp drat wt qsec vs am cyl2 cyl4
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 4 21 6 160 110 3.9 2.62 16.5 0 1 12 24
#> 2 4 4 21 6 160 110 3.9 2.88 17.0 0 1 12 24
#> 3 4 1 22.8 4 108 93 3.85 2.32 18.6 1 1 8 16
#> 4 3 1 21.4 6 258 110 3.08 3.22 19.4 1 0 12 24
#> # … with 28 more rows
# to back
%>% relocate(mpg, cyl, .after = last_col())
mtcars #> # A tibble: 32 × 13
#> disp hp drat wt qsec vs am gear carb cyl2 cyl4 mpg cyl
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 160 110 3.9 2.62 16.5 0 1 4 4 12 24 21 6
#> 2 160 110 3.9 2.88 17.0 0 1 4 4 12 24 21 6
#> 3 108 93 3.85 2.32 18.6 1 1 4 1 8 16 22.8 4
#> 4 258 110 3.08 3.22 19.4 1 0 3 1 12 24 21.4 6
#> # … with 28 more rows
We can replicate this in base R with a little set manipulation:
union(c("gear", "carb"), names(mtcars))]
mtcars[#> # A tibble: 32 × 13
#> gear carb mpg cyl disp hp drat wt qsec vs am cyl2 cyl4
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 4 21 6 160 110 3.9 2.62 16.5 0 1 12 24
#> 2 4 4 21 6 160 110 3.9 2.88 17.0 0 1 12 24
#> 3 4 1 22.8 4 108 93 3.85 2.32 18.6 1 1 8 16
#> 4 3 1 21.4 6 258 110 3.08 3.22 19.4 1 0 12 24
#> # … with 28 more rows
<- c("mpg", "cyl")
to_back c(setdiff(names(mtcars), to_back), to_back)]
mtcars[#> # A tibble: 32 × 13
#> disp hp drat wt qsec vs am gear carb cyl2 cyl4 mpg cyl
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 160 110 3.9 2.62 16.5 0 1 4 4 12 24 21 6
#> 2 160 110 3.9 2.88 17.0 0 1 4 4 12 24 21 6
#> 3 108 93 3.85 2.32 18.6 1 1 4 1 8 16 22.8 4
#> 4 258 110 3.08 3.22 19.4 1 0 3 1 12 24 21.4 6
#> # … with 28 more rows
Moving columns to somewhere in the middle requires a little more set twiddling.
rename()
: Rename variables by namedplyr::rename()
allows you to rename variables by name
or position:
%>% rename(sepal_length = Sepal.Length, sepal_width = 2)
iris #> # A tibble: 150 × 5
#> sepal_length sepal_width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> # … with 146 more rows
Renaming variables by position is straight forward in base R:
<- iris
iris2 names(iris2)[2] <- "sepal_width"
Renaming variables by name requires a bit more work:
names(iris2)[names(iris2) == "Sepal.Length"] <- "sepal_length"
rename_with()
: Rename variables with a functiondplyr::rename_with()
transform column names with a
function:
%>% rename_with(toupper)
iris #> # A tibble: 150 × 5
#> SEPAL.LENGTH SEPAL.WIDTH PETAL.LENGTH PETAL.WIDTH SPECIES
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> # … with 146 more rows
A similar effect can be achieved with setNames()
in base
R:
setNames(iris, toupper(names(iris)))
#> # A tibble: 150 × 5
#> SEPAL.LENGTH SEPAL.WIDTH PETAL.LENGTH PETAL.WIDTH SPECIES
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> # … with 146 more rows
select()
: Select variables by namedplyr::select()
subsets columns by position, name,
function of name, or other property:
%>% select(1:3)
iris #> # A tibble: 150 × 3
#> Sepal.Length Sepal.Width Petal.Length
#> <dbl> <dbl> <dbl>
#> 1 5.1 3.5 1.4
#> 2 4.9 3 1.4
#> 3 4.7 3.2 1.3
#> 4 4.6 3.1 1.5
#> # … with 146 more rows
%>% select(Species, Sepal.Length)
iris #> # A tibble: 150 × 2
#> Species Sepal.Length
#> <fct> <dbl>
#> 1 setosa 5.1
#> 2 setosa 4.9
#> 3 setosa 4.7
#> 4 setosa 4.6
#> # … with 146 more rows
%>% select(starts_with("Petal"))
iris #> # A tibble: 150 × 2
#> Petal.Length Petal.Width
#> <dbl> <dbl>
#> 1 1.4 0.2
#> 2 1.4 0.2
#> 3 1.3 0.2
#> 4 1.5 0.2
#> # … with 146 more rows
%>% select(where(is.factor))
iris #> # A tibble: 150 × 1
#> Species
#> <fct>
#> 1 setosa
#> 2 setosa
#> 3 setosa
#> 4 setosa
#> # … with 146 more rows
Subsetting variables by position is straightforward in base R:
1:3] # single argument selects columns; never drops
iris[#> # A tibble: 150 × 3
#> Sepal.Length Sepal.Width Petal.Length
#> <dbl> <dbl> <dbl>
#> 1 5.1 3.5 1.4
#> 2 4.9 3 1.4
#> 3 4.7 3.2 1.3
#> 4 4.6 3.1 1.5
#> # … with 146 more rows
1:3, , drop = FALSE]
iris[#> # A tibble: 3 × 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
You have two options to subset by name:
c("Species", "Sepal.Length")]
iris[#> # A tibble: 150 × 2
#> Species Sepal.Length
#> <fct> <dbl>
#> 1 setosa 5.1
#> 2 setosa 4.9
#> 3 setosa 4.7
#> 4 setosa 4.6
#> # … with 146 more rows
subset(iris, select = c(Species, Sepal.Length))
#> # A tibble: 150 × 2
#> Species Sepal.Length
#> <fct> <dbl>
#> 1 setosa 5.1
#> 2 setosa 4.9
#> 3 setosa 4.7
#> 4 setosa 4.6
#> # … with 146 more rows
Subsetting by function of name requires a bit of work with
grep()
:
grep("^Petal", names(iris))]
iris[#> # A tibble: 150 × 2
#> Petal.Length Petal.Width
#> <dbl> <dbl>
#> 1 1.4 0.2
#> 2 1.4 0.2
#> 3 1.3 0.2
#> 4 1.5 0.2
#> # … with 146 more rows
And you can use Filter()
to subset by type:
Filter(is.factor, iris)
#> # A tibble: 150 × 1
#> Species
#> <fct>
#> 1 setosa
#> 2 setosa
#> 3 setosa
#> 4 setosa
#> # … with 146 more rows
summarise()
: Reduce multiple values down to a single
valuedplyr::summarise()
computes one or more summaries for
each group:
%>%
mtcars group_by(cyl) %>%
summarise(mean = mean(disp), n = n())
#> # A tibble: 3 × 3
#> cyl mean n
#> <dbl> <dbl> <int>
#> 1 4 105. 11
#> 2 6 183. 7
#> 3 8 353. 14
I think the closest base R equivalent uses by()
.
Unfortunately by()
returns a list of data frames, but you
can combine them back together again with do.call()
and
rbind()
:
<- by(mtcars, mtcars$cyl, function(df) {
mtcars_by with(df, data.frame(cyl = cyl[[1]], mean = mean(disp), n = nrow(df)))
})do.call(rbind, mtcars_by)
#> cyl mean n
#> 4 4 105.1364 11
#> 6 6 183.3143 7
#> 8 8 353.1000 14
aggregate()
comes very close to providing an elegant
answer:
<- aggregate(disp ~ cyl, mtcars, function(x) c(mean = mean(x), n = length(x)))
agg
agg#> cyl disp.mean disp.n
#> 1 4 105.1364 11.0000
#> 2 6 183.3143 7.0000
#> 3 8 353.1000 14.0000
But unfortunately while it looks like there are
disp.mean
and disp.n
columns, it’s actually a
single matrix column:
str(agg)
#> 'data.frame': 3 obs. of 2 variables:
#> $ cyl : num 4 6 8
#> $ disp: num [1:3, 1:2] 105 183 353 11 7 ...
#> ..- attr(*, "dimnames")=List of 2
#> .. ..$ : NULL
#> .. ..$ : chr [1:2] "mean" "n"
You can see a variety of other options at https://gist.github.com/hadley/c430501804349d382ce90754936ab8ec.
slice()
: Choose rows by positionslice()
selects rows with their location:
slice(mtcars, 25:n())
#> # A tibble: 8 × 13
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 cyl4
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 19.2 8 400 175 3.08 3.84 17.0 0 0 3 2 16 32
#> 2 27.3 4 79 66 4.08 1.94 18.9 1 1 4 1 8 16
#> 3 26 4 120. 91 4.43 2.14 16.7 0 1 5 2 8 16
#> 4 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2 8 16
#> # … with 4 more rows
This is straightforward to replicate with [
:
25:nrow(mtcars), , drop = FALSE]
mtcars[#> # A tibble: 8 × 13
#> mpg cyl disp hp drat wt qsec vs am gear carb cyl2 cyl4
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 19.2 8 400 175 3.08 3.84 17.0 0 0 3 2 16 32
#> 2 27.3 4 79 66 4.08 1.94 18.9 1 1 4 1 8 16
#> 3 26 4 120. 91 4.43 2.14 16.7 0 1 5 2 8 16
#> 4 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2 8 16
#> # … with 4 more rows
When we want to merge two data frames, x
and
y
), we have a variety of different ways to bring them
together. Various base R merge()
calls are replaced by a
variety of dplyr join()
functions.
dplyr | base |
---|---|
inner_join(df1, df2) |
merge(df1, df2) |
left_join(df1, df2) |
merge(df1, df2, all.x = TRUE) |
right_join(df1, df2) |
merge(df1, df2, all.y = TRUE) |
full_join(df1, df2) |
merge(df1, df2, all = TRUE) |
semi_join(df1, df2) |
df1[df1$x %in% df2$x, , drop = FALSE] |
anti_join(df1, df2) |
df1[!df1$x %in% df2$x, , drop = FALSE] |
For more information about two-table verbs, see
vignette("two-table")
.
dplyr’s inner_join()
, left_join()
,
right_join()
, and full_join()
add new columns
from y
to x
, matching rows based on a set of
“keys”, and differ only in how missing matches are handled. They are
equivalent to calls to merge()
with various settings of the
all
, all.x
, and all.y
arguments.
The main difference is the order of the rows:
x
data frame.merge()
sorts the key columns.dplyr’s semi_join()
and anti_join()
affect
only the rows, not the columns:
%>% semi_join(band_instruments)
band_members #> Joining, by = "name"
#> # A tibble: 2 × 2
#> name band
#> <chr> <chr>
#> 1 John Beatles
#> 2 Paul Beatles
%>% anti_join(band_instruments)
band_members #> Joining, by = "name"
#> # A tibble: 1 × 2
#> name band
#> <chr> <chr>
#> 1 Mick Stones
They can be replicated in base R with [
and
%in%
:
$name %in% band_instruments$name, , drop = FALSE]
band_members[band_members#> # A tibble: 2 × 2
#> name band
#> <chr> <chr>
#> 1 John Beatles
#> 2 Paul Beatles
!band_members$name %in% band_instruments$name, , drop = FALSE]
band_members[#> # A tibble: 1 × 2
#> name band
#> <chr> <chr>
#> 1 Mick Stones
Semi and anti joins with multiple key variables are considerably more challenging to implement.