diff --git a/NAMESPACE b/NAMESPACE index 2dd1c5e..94690c0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(linkage) export(logml2String) export(lueGenePopData) export(lueNimi) +export(max_MATLAB) export(min_MATLAB) export(noIndex) export(ownNum2Str) @@ -34,6 +35,7 @@ export(randdir) export(repmat) export(rivinSisaltamienMjonojenLkm) export(selvitaDigitFormat) +export(setdiff_MATLAB) export(simulateAllFreqs) export(simulateIndividuals) export(simuloiAlleeli) diff --git a/R/addToSummary.R b/R/addToSummary.R index 1c79147..4affbe9 100644 --- a/R/addToSummary.R +++ b/R/addToSummary.R @@ -1,4 +1,4 @@ -addToSummary <- funciton(logml, partitionSummary, worstIndex) { +addToSummary <- function(logml, partitionSummary, worstIndex) { # Tiedet��n, ett� annettu logml on isompi kuin huonoin arvo # partitionSummary taulukossa. Jos partitionSummary:ss� ei viel� ole # annettua logml arvoa, niin lis�t��n worstIndex:in kohtaan uusi logml ja @@ -6,13 +6,13 @@ addToSummary <- funciton(logml, partitionSummary, worstIndex) { apu <- find(abs(partitionSummary[, 2] - logml) < 1e-5) if (isempty(apu)) { - # Nyt l�ydetty partitio ei ole viel� kirjattuna summaryyn. - npops <- length(unique(PARTITION)) - partitionSummary[worstIndex, 1] <- npops - partitionSummary[worstIndex, 2] <- logml - added <- 1 + # Nyt l�ydetty partitio ei ole viel� kirjattuna summaryyn. + npops <- length(unique(PARTITION)) + partitionSummary[worstIndex, 1] <- npops + partitionSummary[worstIndex, 2] <- logml + added <- 1 } else { - added <- 0 + added <- 0 } return(list(partitionSummary = partitionSummary, added = added)) } \ No newline at end of file diff --git a/R/setdiff.R b/R/setdiff.R new file mode 100644 index 0000000..af199a9 --- /dev/null +++ b/R/setdiff.R @@ -0,0 +1,17 @@ +#' @title Set differences of two arrays +#' @description Loosely replicates the behavior of the homonym Matlab function +#' @param A first array +#' @param B second awway +#' @param legacy if `TRUE`, preserves the behavior of +#' @return +#' @author Waldir Leoncio +#' @export +setdiff_MATLAB <- function(A, B, legacy = FALSE) { + if (is(A, "numeric") & is(B, "numeric")) { + values <- sort(unique(A[is.na(match(A, B))])) + } else if (is(A, "data.frame") & is(B, "data.frame")) { + stop("Not implemented for data frames") + } + # TODO: add support for indices (if necessary) + return(values) +} \ No newline at end of file diff --git a/man/admixture_initialization.Rd b/man/admixture_initialization.Rd new file mode 100644 index 0000000..ae2c9bc --- /dev/null +++ b/man/admixture_initialization.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/admixture_initialization.R +\name{admixture_initialization} +\alias{admixture_initialization} +\title{Seuraavat kolme funktiota liittyvat alkupartition muodostamiseen.} +\usage{ +admixture_initialization(data_matrix, nclusters, Z) +} +\description{ +Seuraavat kolme funktiota liittyvat alkupartition muodostamiseen. +} diff --git a/man/laskeMuutokset4.Rd b/man/laskeMuutokset4.Rd index 7c16a19..5c41852 100644 --- a/man/laskeMuutokset4.Rd +++ b/man/laskeMuutokset4.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/laskeMuutokset4.R +% Please edit documentation in R/laskeMuutokset12345.R \name{laskeMuutokset4} \alias{laskeMuutokset4} \title{Calculate changes?} @@ -20,6 +20,6 @@ laskeMuutokset4(osuus, osuusTaulu, omaFreqs, logml, COUNTS = matrix(0)) \description{ Palauttaa npops*npops taulun, jonka alkio (i,j) kertoo, mik?on muutos logml:ss? mikäli populaatiosta i siirretään osuuden verran -todennäköisyysmassaa populaatioon j. Mikäli populaatiossa i ei ole mitään +todennäköisyysmassaa populaatioon j. Mikäli populaatiossa i ei ole mitään siirrettävää, on vastaavassa kohdassa rivi nollia. } diff --git a/man/max_MATLAB.Rd b/man/max_MATLAB.Rd new file mode 100644 index 0000000..d7fbdf7 --- /dev/null +++ b/man/max_MATLAB.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/min_max_MATLAB.R +\name{max_MATLAB} +\alias{max_MATLAB} +\title{Maximum (MATLAB version)} +\usage{ +max_MATLAB(X, indices = TRUE) +} +\arguments{ +\item{X}{matrix} + +\item{indices}{return indices?} +} +\value{ +Either a list or a vector +} +\description{ +Finds the minimum value for each column of a matrix, potentially returning the indices instead +} +\author{ +Waldir Leoncio +} diff --git a/man/min_MATLAB.Rd b/man/min_MATLAB.Rd index bd1113e..9bb1166 100644 --- a/man/min_MATLAB.Rd +++ b/man/min_MATLAB.Rd @@ -1,11 +1,15 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/min.R, R/min_MATLAB.R +% Please edit documentation in R/min.R, R/min_MATLAB.R, R/min_max_MATLAB.R \name{min_MATLAB} \alias{min_MATLAB} \title{Minimum (MATLAB version)} \usage{ min_MATLAB(X, indices = TRUE) +min_MATLAB(X, indices = TRUE) + +min_MATLAB(X, indices = TRUE) + min_MATLAB(X, indices = TRUE) } \arguments{ @@ -16,15 +20,27 @@ min_MATLAB(X, indices = TRUE) \value{ Either a list or a vector +Either a list or a vector + +Either a list or a vector + Either a list or a vector } \description{ Finds the minimum value for each column of a matrix, potentially returning the indices instead +Finds the minimum value for each column of a matrix, potentially returning the indices instead + +Finds the minimum value for each column of a matrix, potentially returning the indices instead + Finds the minimum value for each column of a matrix, potentially returning the indices instead } \author{ Waldir Leoncio +Waldir Leoncio + +Waldir Leoncio + Waldir Leoncio } diff --git a/man/setdiff_MATLAB.Rd b/man/setdiff_MATLAB.Rd new file mode 100644 index 0000000..681e9f9 --- /dev/null +++ b/man/setdiff_MATLAB.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/setdiff.R +\name{setdiff_MATLAB} +\alias{setdiff_MATLAB} +\title{Set differences of two arrays} +\usage{ +setdiff_MATLAB(A, B, legacy = FALSE) +} +\arguments{ +\item{A}{first array} + +\item{B}{second awway} + +\item{legacy}{if `TRUE`, preserves the behavior of} +} +\value{ + +} +\description{ +Loosely replicates the behavior of the homonym Matlab function +} +\author{ +Waldir Leoncio +} diff --git a/tests/testthat/test-convertedBaseFunctions.R b/tests/testthat/test-convertedBaseFunctions.R index 11988f8..5623e38 100644 --- a/tests/testthat/test-convertedBaseFunctions.R +++ b/tests/testthat/test-convertedBaseFunctions.R @@ -158,12 +158,13 @@ test_that("find works as expected", { }) test_that("sortrows works as expected", { - mx <- matrix(c(3, 2, 2, 1, 1, 10, 0, pi), 4) - expect_equal(sortrows(mx), matrix(c(1, 2, 2, 3, pi, 10, 0, 1), 4)) - expect_equal(sortrows(mx, 2), matrix(c(2, 3, 1, 2, 0, 1, pi, 10), 4)) - expect_equal(sortrows(mx, 1:2), mx[order(mx[, 1], mx[, 2]), ]) + mx <- matrix(c(3, 2, 2, 1, 1, 10, 0, pi), 4) + expect_equal(sortrows(mx), matrix(c(1, 2, 2, 3, pi, 10, 0, 1), 4)) + expect_equal(sortrows(mx, 2), matrix(c(2, 3, 1, 2, 0, 1, pi, 10), 4)) + expect_equal(sortrows(mx, 1:2), mx[order(mx[, 1], mx[, 2]), ]) }) +# FIXME: failing tests test_that("cell works as expected", { expect_equal(cell(0), array(dim = c(0, 0))) expect_equal(cell(1), array(dim = c(1, 1))) @@ -217,4 +218,28 @@ test_that("nargin works correctly", { expect_equal(addme(13, 42), 55) expect_equal(addme(13), 26) expect_equal(addme(), 0) +}) + +test_that("setdiff works as expected", { + A <- c(3, 6, 2, 1, 5, 1, 1) + B <- c(2, 4, 6) + C <- c(1, 3, 5) + expect_equal(setdiff(A, B), C) + A <- data.frame( + Var1 = 1:5, + Var2 = LETTERS[1:5], + Var3 = c(FALSE, TRUE, FALSE, TRUE, FALSE) + ) + B <- data.frame( + Var1 = seq(1, 9, by = 2), + Var2 = LETTERS[seq(1, 9, by = 2)], + Var3 = rep(FALSE, 5) + ) + C <- data.frame( + Var1 = c(2, 4), + Var2 = c('B', 'D'), + Var3 = c(TRUE, TRUE) + ) + expect_equal(setdiff(A, B), C) + # TODO: add more examples from https://se.mathworks.com/help/matlab/ref/double.setdiff.html;jsessionid=0d8d42582d4d299b8224403899f1 }) \ No newline at end of file