diff --git a/NEWS.md b/NEWS.md index 95e8dcbc..51d86689 100644 --- a/NEWS.md +++ b/NEWS.md @@ -24,6 +24,8 @@ - Make the [vdiffr](https://vdiffr.r-lib.org) package optional: only test the plots locally, and only if vdiffr is installed. +- `calc_sdp()` can now take a plain vector (Issue #142). + ### Bug fixes - Fixed [Issue #181](https://github.com/rqtl/qtl2/issues/181), where diff --git a/R/calc_sdp.R b/R/calc_sdp.R index b5ad4e2d..eb6d8437 100644 --- a/R/calc_sdp.R +++ b/R/calc_sdp.R @@ -23,9 +23,14 @@ calc_sdp <- function(geno) { # tolerate data frames, but convert to matrix - if(!is.matrix(geno) && is.data.frame(geno)) - geno <- as.matrix(geno) - if(!is.matrix(geno)) stop("geno should be a matrix") + if(!is.matrix(geno)) { + if(is.data.frame(geno)) { + geno <- as.matrix(geno) + } else { + geno <- rbind(geno) + dimnames(geno) <- NULL + } + } n_str <- ncol(geno) diff --git a/tests/testthat/test-calc_sdp.R b/tests/testthat/test-calc_sdp.R index e7671123..d9d4b453 100644 --- a/tests/testthat/test-calc_sdp.R +++ b/tests/testthat/test-calc_sdp.R @@ -42,6 +42,10 @@ test_that("calc_sdp works", { g <- g[n_AA > 0 & n_AA < 8,] expect_equal(calc_sdp(g), apply(g, 1, function(a) sum(((a-1)/2)*2^(seq(along=a)-1)))) + + expect_equal( calc_sdp( c(1,1,1,3,1,1,1,1) ), 8) + expect_equal( calc_sdp( data.frame(1,1,1,3,1,1,1,1)), 8) + })