Translated BAPS::noIndex into R

This commit is contained in:
Waldir Leoncio 2020-03-03 14:30:39 +01:00
parent 17282c423a
commit 8ecc59a4e6
4 changed files with 53 additions and 0 deletions

View file

@ -11,6 +11,7 @@ export(etsiParas)
export(isfield) export(isfield)
export(laskeMuutokset4) export(laskeMuutokset4)
export(learn_simple_partition) export(learn_simple_partition)
export(noIndex)
export(ownNum2Str) export(ownNum2Str)
export(poistaLiianPienet) export(poistaLiianPienet)
export(proportion2str) export(proportion2str)

19
R/noIndex.R Normal file
View file

@ -0,0 +1,19 @@
#' @title No index
#' @description Checks that the data contains no index column.
#' @details As input, this function takes two variables from a mixture/admixture
#' result structure.
#' @return puredata: a data contains no index column.
#' @export
noIndex <- function (data, noalle) {
limit <- ifelse(is(noalle, "matrix"), ncol(noalle), length(noalle))
if (size(data, 2) == limit + 1) {
if (is(data, "matrix")) {
puredata <- data[, -ncol(data)] # remove the index column
} else {
puredata <- data[-length(data)]
}
} else {
puredata <- data
}
return(puredata)
}

18
man/noIndex.Rd Normal file
View file

@ -0,0 +1,18 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/noIndex.R
\name{noIndex}
\alias{noIndex}
\title{No index}
\usage{
noIndex(data, noalle)
}
\value{
puredata: a data contains no index column.
}
\description{
Checks that the data contains no index column.
}
\details{
As input, this function takes two variables from a mixture/admixture
result structure.
}

View file

@ -233,3 +233,18 @@ test_that("poistaLiianPienet works as expected", {
expect_equal(poistaLiianPienet(100, matrix(1:4, 2), 0), 100) expect_equal(poistaLiianPienet(100, matrix(1:4, 2), 0), 100)
expect_equal(poistaLiianPienet(100, matrix(1:4, 2), -5), 100) expect_equal(poistaLiianPienet(100, matrix(1:4, 2), -5), 100)
}) })
test_that("noIndex works properly", {
abcd_vec <- letters[1:4]
abcd_mat <- matrix(abcd_vec, 2)
abcdef_mat <- matrix(letters[1:6], 2)
efg_vec <- letters[5:7]
expect_equal(noIndex(abcd_vec, 1:6), abcd_vec)
expect_equal(noIndex(abcd_vec, 1:3), abcd_vec[-4])
expect_equal(noIndex(abcd_vec, 1:2), abcd_vec)
expect_equal(noIndex(abcd_vec, efg_vec), abcd_vec[-4])
expect_equal(noIndex(abcd_mat, 1), abcd_mat[, 1])
expect_equal(noIndex(abcd_mat, 2), abcd_mat[, 1])
expect_equal(noIndex(abcdef_mat, 1:2), abcdef_mat[, 1:2])
expect_equal(noIndex(abcdef_mat, abcd_mat), abcdef_mat[, 1:2])
})