diff --git a/R/utils_find_correlationtype.R b/R/utils_find_correlationtype.R index b8a74921..3424a3ed 100644 --- a/R/utils_find_correlationtype.R +++ b/R/utils_find_correlationtype.R @@ -57,7 +57,7 @@ out$is_continuous <- TRUE } - if (all(x %% 1 == 0)) { + if (any(!is.na(x)) && all(x %% 1 == 0, na.rm = TRUE)) { out$is_count <- TRUE } diff --git a/tests/testthat/test-cor_pairwise_complete_with_NA.R b/tests/testthat/test-cor_pairwise_complete_with_NA.R new file mode 100644 index 00000000..c5fd32cc --- /dev/null +++ b/tests/testthat/test-cor_pairwise_complete_with_NA.R @@ -0,0 +1,17 @@ +test_that("pairwise complete correlation with missing data works", { + set.seed(345345) + data_set <- data.frame( + a = sample(c(NA, 1:5), 1000, replace = TRUE), + b = sample(c(NA, 1:5), 1000, replace = TRUE) + ) + + expected_cor <- cor(data_set, use = "pairwise.complete.obs") + + got_cor <- correlation::correlation( + data = data_set, + method = "auto", + missing = "keep_pairwise" + ) + + testthat::expect_equal(got_cor$r, expected_cor[1, 2]) +})