1df_var <- data.frame( 2 l = c(T, F, F), 3 i = c(1, 1, 2), 4 d = Sys.Date() + c(1, 1, 2), 5 f = factor(letters[c(1, 1, 2)]), 6 n = c(1, 1, 2) + 0.5, 7 t = Sys.time() + c(1, 1, 2), 8 c = letters[c(1, 1, 2)], 9 stringsAsFactors = FALSE 10) 11 12test_that("bind_rows handles complex. #933", { 13 df1 <- data.frame(r = c(1 + 1i, 2 - 1i)) 14 df2 <- data.frame(r = c(1 - 1i, 2 + 1i)) 15 df3 <- bind_rows(df1, df2) 16 expect_equal(nrow(df3), 4L) 17 expect_equal(df3$r, c(df1$r, df2$r)) 18}) 19 20test_that("bind_rows is careful about column names encoding #1265", { 21 one <- data.frame(foo = 1:3, bar = 1:3) 22 names(one) <- c("f\u00fc", "bar") 23 two <- data.frame(foo = 1:3, bar = 1:3) 24 names(two) <- c("f\u00fc", "bar") 25 Encoding(names(one)[1]) <- "UTF-8" 26 expect_equal(names(one), names(two)) 27 res <- bind_rows(one, two) 28 expect_equal(ncol(res), 2L) 29}) 30 31test_that("bind_rows handles POSIXct (#1125)", { 32 df1 <- data.frame(date = as.POSIXct(NA)) 33 df2 <- data.frame(date = as.POSIXct("2015-05-05")) 34 res <- bind_rows(df1, df2) 35 expect_equal(nrow(res), 2L) 36 expect_true(is.na(res$date[1])) 37}) 38 39test_that("bind_rows respects ordered factors (#1112)", { 40 l <- c("a", "b", "c", "d") 41 id <- factor(c("a", "c", "d"), levels = l, ordered = TRUE) 42 df <- data.frame(id = rep(id, 2), val = rnorm(6)) 43 res <- bind_rows(df, df) 44 expect_s3_class(res$id, "ordered") 45 expect_equal(levels(df$id), levels(res$id)) 46}) 47 48test_that("bind handles POSIXct of different tz ", { 49 date1 <- structure(-1735660800, tzone = "America/Chicago", class = c("POSIXct", "POSIXt")) 50 date2 <- structure(-1735660800, tzone = "UTC", class = c("POSIXct", "POSIXt")) 51 date3 <- structure(-1735660800, class = c("POSIXct", "POSIXt")) 52 53 df1 <- data.frame(date = date1) 54 df2 <- data.frame(date = date2) 55 df3 <- data.frame(date = date3) 56 57 res <- bind_rows(df1, df2) 58 expect_equal(attr(res$date, "tzone"), "America/Chicago") 59 60 res <- bind_rows(df1, df3) 61 expect_equal(attr(res$date, "tzone"), "America/Chicago") 62 63 res <- bind_rows(df2, df3) 64 expect_equal(attr(res$date, "tzone"), "UTC") 65 66 res <- bind_rows(df3, df3) 67 expect_equal(attr(res$date, "tzone"), "") 68 69 res <- bind_rows(df1, df2, df3) 70 expect_equal(attr(res$date, "tzone"), "America/Chicago") 71}) 72 73test_that("bind_rows() creates a column of identifiers (#1337)", { 74 data1 <- mtcars[c(2, 3), ] 75 data2 <- mtcars[1, ] 76 77 out <- bind_rows(data1, data2, .id = "col") 78 out_list <- bind_rows(list(data1, data2), .id = "col") 79 expect_equal(names(out)[1], "col") 80 expect_equal(out$col, c("1", "1", "2")) 81 expect_equal(out_list$col, c("1", "1", "2")) 82 83 out_labelled <- bind_rows(one = data1, two = data2, .id = "col") 84 out_list_labelled <- bind_rows(list(one = data1, two = data2), .id = "col") 85 expect_equal(out_labelled$col, c("one", "one", "two")) 86 expect_equal(out_list_labelled$col, c("one", "one", "two")) 87}) 88 89test_that("empty data frame are handled (#1346)", { 90 res <- tibble() %>% bind_rows(tibble(x = "a")) 91 expect_equal(nrow(res), 1L) 92}) 93 94test_that("bind_rows handles POSIXct stored as integer (#1402)", { 95 now <- Sys.time() 96 97 df1 <- data.frame(time = now) 98 expect_equal(class(bind_rows(df1)$time), c("POSIXct", "POSIXt")) 99 100 df2 <- data.frame(time = seq(now, length.out = 1, by = 1)) 101 expect_equal(class(bind_rows(df2)$time), c("POSIXct", "POSIXt")) 102 103 res <- bind_rows(df1, df2) 104 expect_equal(class(res$time), c("POSIXct", "POSIXt")) 105 expect_true(all(res$time == c(df1$time, df2$time))) 106}) 107 108test_that("bind_rows() correctly handles consecutive NULLs (#4296)", { 109 res <- list( 110 a = tibble(expected_id = "a"), 111 b = NULL, 112 c = NULL, 113 d = tibble(expected_id = "d"), 114 c = NULL, 115 e = tibble(expected_id = "e") 116 ) %>% 117 bind_rows(.id = "id") 118 expect_equal(res$id, res$expected_id) 119}) 120