| 1 |
|
| 2 |
test_that("bind() handles named lists", { |
| 3 |
|
| 4 |
data <- list( |
| 5 |
alpha = list(A = 1, B = 2), |
| 6 |
beta = list(A = 3, B = 4), |
| 7 |
gamma = list(A = 5, B = 6) |
| 8 |
) |
| 9 |
|
| 10 |
actual <- bind(data) |
| 11 |
expected <- data.frame( |
| 12 |
Index = names(data), |
| 13 |
A = c(1, 3, 5), |
| 14 |
B = c(2, 4, 6), |
| 15 |
stringsAsFactors = FALSE |
| 16 |
) |
| 17 |
|
| 18 |
expect_equal(actual, expected) |
| 19 |
|
| 20 |
}) |
| 21 |
|
| 22 |
test_that("bind() warns on name collision", { |
| 23 |
data <- list(alpha = list(Index = 1), beta = list(Index = 2)) |
| 24 |
expect_error(bind(data)) |
| 25 |
}) |
| 26 |
|
| 27 |
test_that("bind() handles data.frames with potentially different names", { |
| 28 |
|
| 29 |
data <- list( |
| 30 |
a = data.frame(A = 1, B = 2), |
| 31 |
b = data.frame(A = 1, C = 3) |
| 32 |
) |
| 33 |
|
| 34 |
bound <- bind(data) |
| 35 |
expected <- data.frame( |
| 36 |
Index = c("a", "b"), |
| 37 |
A = c(1, 1), |
| 38 |
B = c(2, NA), |
| 39 |
C = c(NA, 3), |
| 40 |
stringsAsFactors = FALSE |
| 41 |
) |
| 42 |
|
| 43 |
expect_identical(bound, expected) |
| 44 |
|
| 45 |
}) |
| 46 |
|
| 47 |
test_that("bind() preserves order where possible", { |
| 48 |
|
| 49 |
data <- list( |
| 50 |
a = data.frame(A = 1, C = 3), |
| 51 |
b = data.frame(A = 1, B = 2, C = 3) |
| 52 |
) |
| 53 |
|
| 54 |
bound <- bind(data) |
| 55 |
expect_equal(names(bound), c("Index", "A", "B", "C")) |
| 56 |
|
| 57 |
}) |
| 58 |
|
| 59 |
test_that("bind() handles unnamed lists with explicit name", { |
| 60 |
|
| 61 |
data <- list( |
| 62 |
list(1, 2, 3), |
| 63 |
list(4, 5, 6), |
| 64 |
list(7, 8, 9) |
| 65 |
) |
| 66 |
|
| 67 |
actual <- bind(data, names = c("A", "B", "C")) |
| 68 |
|
| 69 |
expected <- data.frame( |
| 70 |
A = c(1, 4, 7), |
| 71 |
B = c(2, 5, 8), |
| 72 |
C = c(3, 6, 9) |
| 73 |
) |
| 74 |
|
| 75 |
expect_equal(actual, expected) |
| 76 |
|
| 77 |
}) |
| 78 |
|