diff --git a/R/assortativity.R b/R/assortativity.R index 912cd9ae3c6..4d94ab7b564 100644 --- a/R/assortativity.R +++ b/R/assortativity.R @@ -237,7 +237,8 @@ assortativity_legacy <- function( #' @param types Vector giving the vertex types. They as assumed to be integer #' numbers, starting with one. Non-integer values are converted to integers -#' with [as.integer()]. +#' with [as.integer()]. Character vectors are converted to integers using +#' [as.factor()]. #' @rdname assortativity #' @export #' @cdocs igraph_assortativity_nominal @@ -247,6 +248,11 @@ assortativity_nominal <- function( directed = TRUE, normalized = TRUE ) { + # Convert character types to factor then to integer for categorical data + if (is.character(types)) { + types <- as.integer(as.factor(types)) + } + assortativity_nominal_impl( graph = graph, types = types, diff --git a/man/assortativity.Rd b/man/assortativity.Rd index 038771a866a..a5169519c23 100644 --- a/man/assortativity.Rd +++ b/man/assortativity.Rd @@ -54,7 +54,8 @@ Deprecated aliases for \code{values} and \code{values.in}, respectively.} \item{types}{Vector giving the vertex types. They as assumed to be integer numbers, starting with one. Non-integer values are converted to integers -with \code{\link[=as.integer]{as.integer()}}.} +with \code{\link[=as.integer]{as.integer()}}. Character vectors are converted to integers using +\code{\link[=as.factor]{as.factor()}}.} } \value{ A single real number. diff --git a/man/assortativity.nominal.Rd b/man/assortativity.nominal.Rd index 08db8faf436..f42010bf6c3 100644 --- a/man/assortativity.nominal.Rd +++ b/man/assortativity.nominal.Rd @@ -11,7 +11,8 @@ assortativity.nominal(graph, types, directed = TRUE, normalized = TRUE) \item{types}{Vector giving the vertex types. They as assumed to be integer numbers, starting with one. Non-integer values are converted to integers -with \code{\link[=as.integer]{as.integer()}}.} +with \code{\link[=as.integer]{as.integer()}}. Character vectors are converted to integers using +\code{\link[=as.factor]{as.factor()}}.} \item{directed}{Logical scalar, whether to consider edge directions for directed graphs. diff --git a/tests/testthat/test-assortativity.R b/tests/testthat/test-assortativity.R index 77915bf3090..6fd65d37d53 100644 --- a/tests/testthat/test-assortativity.R +++ b/tests/testthat/test-assortativity.R @@ -52,3 +52,32 @@ test_that("nominal assortativity works", { expect_equal(nominal_assortativity, reference_nominal_assortativity) }) + +test_that("nominal assortativity works with character types", { + set.seed(2) + g <- sample_gnm(10, 20) + + # Test with numeric types + V(g)$random1 <- sample(c(1, 2), 10, replace = TRUE) + result1 <- assortativity_nominal(g, types = V(g)$random1) + expect_type(result1, "double") + expect_false(is.na(result1)) + + # Test with string numeric types + V(g)$random2 <- sample(c('1', '2'), 10, replace = TRUE) + result2 <- assortativity_nominal(g, types = V(g)$random2) + expect_type(result2, "double") + expect_false(is.na(result2)) + + # Test with string letter types - this was failing before the fix + V(g)$random3 <- sample(c('A', 'B'), 10, replace = TRUE) + result3 <- assortativity_nominal(g, types = V(g)$random3) + expect_type(result3, "double") + expect_false(is.na(result3)) + + # Verify that equivalent representations produce the same result + # Convert character labels to numeric equivalents + numeric_from_char <- as.integer(as.factor(V(g)$random3)) + result_numeric <- assortativity_nominal(g, types = numeric_from_char) + expect_equal(result3, result_numeric) +})