diff --git a/R/utility.R b/R/utility.R index d85cff9..e09d70c 100644 --- a/R/utility.R +++ b/R/utility.R @@ -26,7 +26,7 @@ as_cordf <- function(x, diagonal = NA) { stop("Input object x is not a square. ", "The number of columns must be equal to the number of rows.") } - diag(x) <- diagonal + if (ncol(x) > 1) diag(x) <- diagonal new_cordf(x, names(x)) } diff --git a/tests/testthat/test-correlate.R b/tests/testthat/test-correlate.R index ea970f1..5062d56 100644 --- a/tests/testthat/test-correlate.R +++ b/tests/testthat/test-correlate.R @@ -18,3 +18,16 @@ test_that("Diagonal sets correctly", { expect_equal(all(is.na(diag(as.matrix(correlate(d, diagonal = NA)[, -1])))), TRUE) expect_equal(all(diag(as.matrix(correlate(d, diagonal = 100)[, -1] == 100))), TRUE) }) + + +test_that("correlate works with numeric vectors", { + expect_equal(correlate(x = 1:10, y = 1:10)[[2]], 1) + expect_equal(correlate(x = 1:10, y = -(1:10), diagonal = 0)[[2]], -1) +}) + +test_that("correlate works with a one-column data.frame", { + var <- "Sepal.Length" + expect_equal(correlate(datasets::iris[var])[[1]], var) + expect_equal(correlate(datasets::iris[var])[[2]], 1) + +})