Skip to content

Commit 4e569f5

Browse files
committed
Add rbind and cbind methods
Fixes #2138
1 parent a3348fd commit 4e569f5

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ S3method(auto_copy,tbl_df)
3131
S3method(auto_copy,tbl_sql)
3232
S3method(c,sql)
3333
S3method(cbind,grouped_df)
34+
S3method(cbind,tbl_df)
3435
S3method(collapse,data.frame)
3536
S3method(collapse,tbl_sql)
3637
S3method(collect,data.frame)
@@ -194,6 +195,7 @@ S3method(print,tbl_lazy)
194195
S3method(print,tbl_sql)
195196
S3method(query,DBIConnection)
196197
S3method(rbind,grouped_df)
198+
S3method(rbind,tbl_df)
197199
S3method(recode,character)
198200
S3method(recode,factor)
199201
S3method(recode,numeric)

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# dplyr 0.5.0.9000
22

3+
* `tbl_df` gains `rbind()` and `cbind()` methods that call `bind_rows()` and
4+
`bind_cols()` respectively (#2138)
5+
36
* `copy_to()` gains an `overwrite` argument which allows you to overwrite
47
an existing table. Use with care! (#2296)
58

R/bind.r

+9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ bind_rows <- function(..., .id = NULL) {
8181
bind_rows_(x, .id)
8282
}
8383

84+
#' @export
85+
rbind.tbl_df <- function(..., deparse.level = 1) {
86+
bind_rows(...)
87+
}
8488

8589
#' @export
8690
#' @rdname bind
@@ -89,6 +93,11 @@ bind_cols <- function(...) {
8993
cbind_all(x)
9094
}
9195

96+
#' @export
97+
cbind.tbl_df <- function(..., deparse.level = 1) {
98+
bind_cols(...)
99+
}
100+
92101
#' @export
93102
#' @rdname bind
94103
combine <- function(...) {

tests/testthat/test-binds.R

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
context("binds")
22

3+
4+
# base --------------------------------------------------------------------
5+
6+
test_that("cbind and rbind methods work for tbl_df", {
7+
df1 <- tibble(x = 1)
8+
df2 <- tibble(y = 2)
9+
10+
expect_equal(cbind(df1, df2), tibble(x = 1, y = 2))
11+
expect_equal(rbind(df1, df2), tibble(x = c(1, NA), y = c(NA, 2)))
12+
})
13+
314
# columns -----------------------------------------------------------------
415

516
test_that("cbind uses shallow copies", {

0 commit comments

Comments
 (0)