1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
test_that("rownames_as_column works", {
test <- rownames_as_column(mtcars, "new_column")
expect_true("new_column" %in% names(test))
expect_identical(test[1, "new_column"], "Mazda RX4")
})
test_that("rownames_as_column doesn't work if var is not a character", {
expect_error(rownames_as_column(mtcars, var = 1),
regexp = "Argument 'var' must be of type character"
)
expect_error(rownames_as_column(mtcars, var = TRUE),
regexp = "Argument 'var' must be of type character"
)
})
test_that("rownames_as_column uses 'rowname' as default column name", {
test <- rownames_as_column(mtcars, var = NULL)
expect_true("rowname" %in% names(test))
})
test_that("rownames_as_column preserves labels", {
test_data <- mtcars
test_data <- assign_labels(test_data, select = "hp", variable = "horsepower")
# ungrouped
with_id <- rownames_as_column(test_data)
expect_identical(
attributes(with_id$hp)$label,
"horsepower"
)
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- rownames_as_column(with_id_grouped)
expect_identical(
attributes(with_id_grouped$hp)$label,
"horsepower"
)
})
test_that("rownames_as_column preserves other attribs", {
test_data <- standardize(mtcars)
# ungrouped
with_id <- rownames_as_column(test_data)
expect_false(is.null(attributes(with_id)$center))
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- rownames_as_column(with_id_grouped)
expect_false(is.null(attributes(with_id_grouped)$center))
})
test_that("rownames_as_column errors if already var of same name", {
expect_error(
rownames_as_column(mtcars, "mpg"),
"already a variable named"
)
})
#-------------------------------------------------
test_that("rowid_as_column works", {
test <- rowid_as_column(mtcars, "new_column")
expect_true("new_column" %in% names(test))
expect_identical(test$new_column, 1:32)
})
test_that("rowid_as_column works with grouped data", {
test_data <- data_group(iris, "Species")
test <- rowid_as_column(test_data)
expect_identical(test$rowid, rep(1:50, 3))
expect_true("rowid" %in% names(test))
})
test_that("rowid_as_column doesn't work if var is not a character", {
expect_error(rowid_as_column(mtcars, var = 1),
regexp = "Argument 'var' must be of type character"
)
expect_error(rowid_as_column(mtcars, var = TRUE),
regexp = "Argument 'var' must be of type character"
)
})
test_that("rowid_as_column uses 'rowid' as default column name", {
test <- rowid_as_column(mtcars, var = NULL)
expect_true("rowid" %in% names(test))
})
test_that("rowid_as_column preserves labels", {
test_data <- mtcars
test_data <- assign_labels(test_data, select = "hp", variable = "horsepower")
# ungrouped
with_id <- rowid_as_column(test_data)
expect_identical(
attributes(with_id$hp)$label,
"horsepower"
)
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- rowid_as_column(with_id_grouped)
expect_identical(
attributes(with_id_grouped$hp)$label,
"horsepower"
)
})
test_that("rowid_as_column preserves other attribs", {
test_data <- standardize(mtcars)
# ungrouped
with_id <- rowid_as_column(test_data)
expect_false(is.null(attributes(with_id)$center))
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- rowid_as_column(with_id_grouped)
expect_false(is.null(attributes(with_id_grouped)$center))
})
test_that("rowid_as_column has no issue if another variable is called 'var'", {
foo <- data.frame(
grp = c("A", "A", "B", "B"),
var = 1:4,
stringsAsFactors = FALSE
)
out <- data_group(foo, grp)
out <- rowid_as_column(out)
expect_named(out, c("rowid", "grp", "var"))
})
test_that("rowid_as_column errors if already var of same name", {
expect_error(
rowid_as_column(mtcars, "mpg"),
"already a variable named"
)
})
#-------------------------------------------------
test_that("column_as_rownames works", {
continents <- c("Africa", "Asia", "Europe", "North America", "Oceania", "South America")
test <- data.frame(
continent = continents,
some_value = seq(1, 6, by = 1)
)
test2 <- column_as_rownames(test, "continent")
expect_identical(rownames(test2), continents)
expect_identical(ncol(test2), 1L)
test3 <- column_as_rownames(test, 1)
expect_identical(rownames(test3), continents)
expect_identical(ncol(test3), 1L)
})
test_that("column_as_rownames sanity checks work", {
continents <- c("Africa", "Asia", "Europe", "North America", "Oceania", "South America")
test <- data.frame(
continent = continents,
some_value = seq(1, 6, by = 1)
)
expect_error(column_as_rownames(test, TRUE),
regexp = "Argument `var`"
)
expect_error(column_as_rownames(test, "foo"),
regexp = "not in the data frame"
)
expect_error(column_as_rownames(test, 0),
regexp = "does not exist"
)
expect_error(column_as_rownames(test, 3),
regexp = "does not exist"
)
})
test_that("rownames_as_column and column_as_rownames cancel each other", {
test <- rownames_as_column(mtcars)
test2 <- column_as_rownames(test)
expect_identical(test2, mtcars)
})
test_that("column_as_rownames preserves labels", {
test_data <- rownames_as_column(mtcars)
test_data <- assign_labels(test_data, select = "hp", variable = "horsepower")
# ungrouped
with_id <- column_as_rownames(test_data)
expect_identical(
attributes(with_id$hp)$label,
"horsepower"
)
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- column_as_rownames(with_id_grouped)
expect_identical(
attributes(with_id_grouped$hp)$label,
"horsepower"
)
})
test_that("column_as_rownames preserves other attribs", {
test_data <- rownames_as_column(standardize(mtcars))
# ungrouped
with_id <- column_as_rownames(test_data, "rowname")
expect_false(is.null(attributes(with_id)$center))
# grouped
with_id_grouped <- data_group(test_data, "cyl")
with_id_grouped <- column_as_rownames(with_id_grouped)
expect_false(is.null(attributes(with_id_grouped)$center))
})
|