typestar

Combining tables in R

bind_rows stacks, bind_cols widens, and both check what they can.

library(dplyr)

july <- tibble(lang = c("r", "python"), runs = c(12L, 15L))
august <- tibble(lang = c("r", "sql"), runs = c(9L, 4L), note = c("a", "b"))

print(bind_rows(july, august))
print(bind_rows(jul = july, aug = august, .id = "month"))
print(bind_cols(july, tibble(mean_tpm = c(92.5, 101.0))))
print(setdiff(names(august), names(july)))

How it works

  1. bind_rows fills missing columns with NA.
  2. .id records which input each row came from.
  3. bind_cols requires the same number of rows.

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
343
Tokens
124
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 71 seconds.

Type this snippet

Step 3 of 4 in Joining, step 23 of 27 in dplyr.

← Previous Next →