Translated function, added tests
This commit is contained in:
parent
c72d2b6896
commit
9e434c25e5
5 changed files with 55 additions and 17 deletions
|
|
@ -9,4 +9,5 @@ export(ownNum2Str)
|
||||||
export(proportion2str)
|
export(proportion2str)
|
||||||
export(randdir)
|
export(randdir)
|
||||||
export(repmat)
|
export(repmat)
|
||||||
|
export(suoritaMuutos)
|
||||||
importFrom(stats,runif)
|
importFrom(stats,runif)
|
||||||
|
|
|
||||||
16
R/admix1.R
16
R/admix1.R
|
|
@ -521,19 +521,3 @@ admix1 <- function(tietue) {
|
||||||
# pointer = pointer+1;
|
# pointer = pointer+1;
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# %--------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
# function osuusTaulu = suoritaMuutos(osuusTaulu, osuus, indeksi)
|
|
||||||
# % Päivittää osuusTaulun muutoksen jälkeen.
|
|
||||||
|
|
||||||
# global COUNTS;
|
|
||||||
# npops = size(COUNTS,3);
|
|
||||||
|
|
||||||
# i1 = rem(indeksi,npops);
|
|
||||||
# if i1==0, i1 = npops; end;
|
|
||||||
# i2 = ceil(indeksi / npops);
|
|
||||||
|
|
||||||
# osuusTaulu(i1) = osuusTaulu(i1)-osuus;
|
|
||||||
# osuusTaulu(i2) = osuusTaulu(i2)+osuus;
|
|
||||||
19
R/suoritaMuutos.R
Normal file
19
R/suoritaMuutos.R
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#' @title suoritaMuutos
|
||||||
|
#' @description Päivittää osuusTaulun muutoksen jälkeen.
|
||||||
|
#' @param osuusTaulu Percentage table?
|
||||||
|
#' @param osuus percentage?
|
||||||
|
#' @param indeksi index
|
||||||
|
#' @param COUNTS counts
|
||||||
|
#' @export
|
||||||
|
suoritaMuutos <- function (osuusTaulu, osuus, indeksi, COUNTS = matrix(0)) {
|
||||||
|
npops <- ifelse(is.na(dim(COUNTS)[3]), 1, dim(COUNTS)[3])
|
||||||
|
|
||||||
|
i1 <- indeksi %% npops
|
||||||
|
if (is.na(i1) | i1 == 0) i1 <- npops
|
||||||
|
i2 <- ceiling(indeksi / npops)
|
||||||
|
|
||||||
|
osuusTaulu[i1] <- osuusTaulu[i1] - osuus
|
||||||
|
osuusTaulu[i2] <- osuusTaulu[i2] + osuus
|
||||||
|
|
||||||
|
return (osuusTaulu)
|
||||||
|
}
|
||||||
20
man/suoritaMuutos.Rd
Normal file
20
man/suoritaMuutos.Rd
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/suoritaMuutos.R
|
||||||
|
\name{suoritaMuutos}
|
||||||
|
\alias{suoritaMuutos}
|
||||||
|
\title{suoritaMuutos}
|
||||||
|
\usage{
|
||||||
|
suoritaMuutos(osuusTaulu, osuus, indeksi, COUNTS = matrix(0))
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{osuusTaulu}{Percentage table?}
|
||||||
|
|
||||||
|
\item{osuus}{percentage?}
|
||||||
|
|
||||||
|
\item{indeksi}{index}
|
||||||
|
|
||||||
|
\item{COUNTS}{counts}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Päivittää osuusTaulun muutoksen jälkeen.
|
||||||
|
}
|
||||||
|
|
@ -118,3 +118,17 @@ test_that("computeIndLogml works like on Matlab", {
|
||||||
expect_equivalent(computeIndLogml(2:3, 2), 2.3026, tol = .0001)
|
expect_equivalent(computeIndLogml(2:3, 2), 2.3026, tol = .0001)
|
||||||
expect_equivalent(computeIndLogml(matrix(8:5, 2), 100), 14.316, tol = .001)
|
expect_equivalent(computeIndLogml(matrix(8:5, 2), 100), 14.316, tol = .001)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test_that("suoritaMuutos works like on Matlab", {
|
||||||
|
mx1 <- c(10, 5, 8)
|
||||||
|
mx2 <- matrix(c(10, 9, 5, 8, 8, -7), 2)
|
||||||
|
expect_equal(suoritaMuutos(10, 3, 1), 10)
|
||||||
|
expect_equal(suoritaMuutos(mx1, 3, 1), c(10, 5, 8))
|
||||||
|
expect_equal(suoritaMuutos(mx1, 3, 2), c(7, 8, 8))
|
||||||
|
expect_equal(suoritaMuutos(mx1, 3, 3), c(7, 5, 11))
|
||||||
|
expect_equal(suoritaMuutos(mx1, 2, 3), c(8, 5, 10))
|
||||||
|
expect_equal(suoritaMuutos(mx1, -7, 3), c(17, 5, 1))
|
||||||
|
expect_equal(suoritaMuutos(mx2, 0, 5), mx2)
|
||||||
|
expect_equal(suoritaMuutos(mx2, 0, 5), mx2)
|
||||||
|
expect_equal(suoritaMuutos(mx2, -3, 6), matrix(c(13, 9, 5, 8, 8, -10), 2))
|
||||||
|
})
|
||||||
Loading…
Add table
Reference in a new issue