if(!require(aaltobda)){ install.packages("remotes") remotes::install_github("avehtari/BDA_course_Aalto", subdir = "rpackage", upgrade="never") library(aaltobda) } mean_vector = c(0, 10) covariance_matrix = matrix(c(4, 12, 12, 100), nrow=2, ncol=2) to_calculate = matrix(c(1, 2, 3, 4), nrow=2, ncol=2) print(class(to_calculate)) # prints: [1] "matrix" "array" # We can force to_calculate to have only one class by uncommenting the line below. #attr(to_calculate, "class") = 'matrix' # If we don't force the to_calculate matrix to have only one class, # we get this error when calling dmvnorm: # Error in if (class(x) == "matrix") { : the condition has length > 1 marginals = dmvnorm(to_calculate, mean_vector, covariance_matrix, log = TRUE) print(marginals) # This code demonstrates the problem: if (class(to_calculate) == 'matrix'){ print(1) } else { print(2) } # The code can be fixed like this: if ('matrix' %in% class(to_calculate)){ print(1) } else { print(2) }