Skip to content

Commit

Permalink
Fix subsetting that failed on single column df. Close tidymodels#119
Browse files Browse the repository at this point in the history
  • Loading branch information
antoine-sachet committed Oct 28, 2020
1 parent ab056fc commit 170e571
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions R/cor_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ as_matrix.cor_df <- function(x, diagonal) {

# Separate rownames
row_name <- x$rowname
x <- x[, colnames(x) != "rowname"]
x <- x[colnames(x) != "rowname"]
# Convert to matrix and set rownames
class(x) <- "data.frame"
x <- as.matrix(x)
Expand All @@ -22,7 +22,7 @@ shave.cor_df <- function(x, upper = TRUE) {

# Separate rownames
row_name <- x$rowname
x <- x[, colnames(x) != "rowname"]
x <- x[colnames(x) != "rowname"]

# Remove upper matrix
if (upper) {
Expand Down Expand Up @@ -93,7 +93,7 @@ focus_if.cor_df <- function(x, .predicate, ..., mirror = FALSE) {

# Identify which variables to keep
to_keep <- map_lgl(
x[, colnames(x) != "rowname"],
x[colnames(x) != "rowname"],
.predicate, ...
)

Expand Down
2 changes: 1 addition & 1 deletion R/reshape.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ stretch <- function(x, na.rm = FALSE, remove.dups = FALSE) {
stretch.cor_df <- function(x, na.rm = FALSE, remove.dups = FALSE) {
if(remove.dups) x <- shave(x)
row_name <- x$rowname
x <- x[, colnames(x) != "rowname"]
x <- x[colnames(x) != "rowname"]
tb <- imap_dfr(x, ~tibble(x = .y, y = row_name, r = .x))
if(na.rm) tb <- tb[!is.na(tb$r), ]
if(remove.dups) {
Expand Down
2 changes: 1 addition & 1 deletion R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ as_cordf <- function(x, diagonal = NA) {
}
x <- as.data.frame(x)
row_name <- x$rowname
x <- x[, colnames(x) != "rowname"]
x <- x[colnames(x) != "rowname"]
rownames(x) <- row_name
if(ncol(x) != nrow(x)) {
stop("Input object x is not a square. ",
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-as_cordf.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ test_that("Diagonal sets correctly", {
expect_equal(all(is.na(diag(as.matrix(as_cordf(d, diagonal = NA)[, -1])))), TRUE)
expect_equal(all(diag(as.matrix(as_cordf(d, diagonal = 100)[, -1] == 100))), TRUE)
})

test_that("as_cordf handles single correlation", {
d1 <- cor(mtcars["cyl"])
expect_s3_class(as_cordf(d1), "cor_df")
expect_equal(colnames(as_cordf(d1)), c("rowname", colnames(d1)))
})

0 comments on commit 170e571

Please sign in to comment.