Translated ownNum2Str

This commit is contained in:
Waldir Leoncio 2019-12-16 17:47:04 +01:00
parent 98691963ec
commit 09c9f0924b
5 changed files with 66 additions and 38 deletions

View file

@ -3,3 +3,4 @@
export(admix1) export(admix1)
export(calculatePopLogml) export(calculatePopLogml)
export(learn_simple_partition) export(learn_simple_partition)
export(ownNum2Str)

View file

@ -770,39 +770,3 @@ admix1 <- function(tietue) {
# %-------------------------------------------------------------------------- # %--------------------------------------------------------------------------
# %----- # %-----
# function str = ownNum2Str(number)
# absolute = abs(number);
# if absolute < 1000
# str = num2str(number);
# elseif absolute < 10000000
# first_three = rem(number,1000);
# next_four = (number - first_three) /1000;
# first_three = abs(first_three);
# if first_three<10
# first_three = ['00' num2str(first_three)];
# elseif first_three<100
# first_three = ['0' num2str(first_three)];
# else
# first_three = num2str(first_three);
# end;
# str = [num2str(next_four) first_three];
# elseif absolute < 100000000
# first_four = rem(number,10000);
# next_four = (number - first_four) /10000;
# first_four = abs(first_four);
# if first_four<10
# first_four = ['000' num2str(first_four)];
# elseif first_four<100
# first_four = ['00' num2str(first_four)];
# elseif first_four<1000
# first_four = ['0' num2str(first_four)];
# else
# first_four = num2str(first_four);
# end;
# str = [num2str(next_four) first_four];
# else
# str = num2str(number);
# end;

39
R/ownNum2Str.R Normal file
View file

@ -0,0 +1,39 @@
#' @title Own number to string
#' @param number number
#' @note On Matlab, if number is NaN the output is 'NaN'. Here, the output will be an error. Also, the function belo expects "number" to have length one, whereas Matlab accepts vectors.
#' @export
ownNum2Str <- function(number) {
absolute <- abs(number)
if (absolute < 1000) {
str <- as.character(number)
} else if (absolute < 10000000) {
first_three <- number %% 1000
next_four <- (number - first_three) /1000
first_three <- abs(first_three)
if (first_three < 10) {
first_three <- paste0('00', as.character(first_three))
} else if (first_three < 100) {
first_three <- paste0('0', as.character(first_three))
} else {
first_three <- as.character(first_three)
}
str <- paste0(as.character(next_four), first_three)
} else if (absolute < 100000000) {
first_four <- number %% 10000
next_four <- (number - first_four) / 10000
first_four <- abs(first_four)
if (first_four < 10) {
first_four <- paste0('000', as.character(first_four))
} else if (first_four < 100) {
first_four <- paste0('00', as.character(first_four))
} else if (first_four < 1000) {
first_four <- paste0('0', as.character(first_four))
} else {
first_four <- as.character(first_four)
}
str <- paste0(as.character(next_four), first_four)
} else {
str <- as.character(number)
}
return(str)
}

17
man/ownNum2Str.Rd Normal file
View file

@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ownNum2Str.R
\name{ownNum2Str}
\alias{ownNum2Str}
\title{Own number to string}
\usage{
ownNum2Str(number)
}
\arguments{
\item{number}{number}
}
\description{
Own number to string
}
\note{
On Matlab, if number is NaN the output is 'NaN'. Here, the output will be an error. Also, the function belo expects "number" to have length one, whereas Matlab accepts vectors.
}

View file

@ -1,7 +1,7 @@
context("Admixture analysis") context("Admixture analysis")
test_that("learn*partition behaves like Matlab", { test_that("learn*partition behaves like on Matlab", {
# Test data # Test data
p1 <- c(0, .5, 1, 1.5) p1 <- c(0, .5, 1, 1.5)
p2 <- c(seq(0, .5, .1), 1, 1, 1, 2) p2 <- c(seq(0, .5, .1), 1, 1, 1, 2)
@ -36,3 +36,10 @@ test_that("learn*partition behaves like Matlab", {
expected = matrix(c(1, 2, 2, 2)) expected = matrix(c(1, 2, 2, 2))
) )
}) })
test_that("ownNum2Str behaves like on Matlab", {
expect_equal(ownNum2Str(1), "1")
expect_equal(ownNum2Str(-123456789), "-123456789")
expect_equal(ownNum2Str(0), "0")
expect_error(ownNum2Str("a"))
})