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
bind_rowsfills missing columns with NA..idrecords which input each row came from.bind_colsrequires the same number of rows.
Keywords and builtins used here
bind_colsbind_rowsclibrarynamesprintsetdifftibble
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.
Step 3 of 4 in Joining, step 23 of 27 in dplyr.