Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions R/data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ as.table.datawizard_crosstab <- function(
if (.is_grouped_df_xtab(x)) {
x$Group <- NULL
}

# first column contains the row names
row_names <- as.character(x[[1]])
row_names[is.na(row_names)] <- "NA"
Expand All @@ -805,6 +806,10 @@ as.table.datawizard_crosstab <- function(
}
# coerce to table
result <- as.table(as.matrix(x))

dimnames_names <- c(attr(x, "varname"), attr(x, "by"))
names(attributes(result)$dimnames) <- dimnames_names

# if we don't want to simplify the table, we wrap it into a list
if (!simplify) {
result <- list(result)
Expand Down
70 changes: 39 additions & 31 deletions tests/testthat/_snaps/data_tabulate.md
Original file line number Diff line number Diff line change
Expand Up @@ -925,59 +925,65 @@
as.table(x)
Output
[[1]]
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
mtcars$gear
mtcars$cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2


---

Code
as.table(x, simplify = TRUE)
Output
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
mtcars$gear
mtcars$cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2

---

Code
as.table(x)
Output
[[1]]
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
gear
cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2


---

Code
as.table(x, simplify = TRUE)
Output
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
gear
cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2

---

Code
as.table(x)
Output
[[1]]
3 4 5
0 15 4 0
1 0 8 5
gear
am 3 4 5
0 15 4 0
1 0 8 5

[[2]]
3 4 5
4 1 8 2
6 2 4 1
8 12 0 2
gear
cyl 3 4 5
4 1 8 2
6 2 4 1
8 12 0 2


---
Expand All @@ -986,15 +992,17 @@
as.table(x)
Output
$`am (0)`
3 4
4 1 2
6 2 2
8 12 0
gear
cyl 3 4
4 1 2
6 2 2
8 12 0

$`am (1)`
4 5
4 6 2
6 2 1
8 0 2
gear
cyl 4 5
4 6 2
6 2 1
8 0 2


1 change: 1 addition & 0 deletions tests/testthat/test-data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,7 @@ test_that("data_tabulate, table methods", {
x <- data_tabulate(mtcars$cyl, mtcars$gear)
expect_type(as.table(x), "list")
expect_s3_class(as.table(x, simplify = TRUE), "table")
expect_named(dimnames(as.table(x)[[1]]), c("mtcars$cyl", "mtcars$gear"))
expect_snapshot(as.table(x))
expect_snapshot(as.table(x, simplify = TRUE))

Expand Down
Loading