From 11efa94ee895ae7a405e4d2e78deb2cde976c32c Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 17 Dec 2019 16:38:55 +0100 Subject: [PATCH] Added proportion2str --- NAMESPACE | 1 + R/admix1.R | 21 +-------------------- R/proportion2str.R | 21 +++++++++++++++++++++ TODO.md | 6 +++++- man/proportion2str.Rd | 21 +++++++++++++++++++++ tests/testthat/test-admix1.R | 8 +++++++- 6 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 R/proportion2str.R create mode 100644 man/proportion2str.Rd diff --git a/NAMESPACE b/NAMESPACE index 100f28c..1487b23 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(calculatePopLogml) export(computeRows) export(learn_simple_partition) export(ownNum2Str) +export(proportion2str) export(randdir) export(repmat) importFrom(stats,runif) diff --git a/R/admix1.R b/R/admix1.R index 3c0eef9..ffa476d 100644 --- a/R/admix1.R +++ b/R/admix1.R @@ -674,23 +674,4 @@ admix1 <- function(tietue) { # fclose(fid); # else # diary off -# end - -# %------------------------------------------------------ - -# function str = proportion2str(prob) -# %prob belongs to [0.00, 0.01, ... ,1]. -# %str is a 4-mark presentation of proportion. - -# if abs(prob)<1e-3 -# str = '0.00'; -# elseif abs(prob-1) < 1e-3; -# str = '1.00'; -# else -# prob = round(100*prob); -# if prob<10 -# str = ['0.0' num2str(prob)]; -# else -# str = ['0.' num2str(prob)]; -# end; -# end; \ No newline at end of file +# end \ No newline at end of file diff --git a/R/proportion2str.R b/R/proportion2str.R new file mode 100644 index 0000000..2dfe9df --- /dev/null +++ b/R/proportion2str.R @@ -0,0 +1,21 @@ +#' @title Convert proportion to string +#' @param prob belongs to [0.00, 0.01, ... ,1] +#' @return a 4-mark presentation of proportion +#' @note The `round` function in R, being ISO-compliant, rounds 8.5 to 8. The +#' Matlab equivalent rounds it to 9. +#' @export +proportion2str <- function (prob) { + if (abs(prob) < 1e-3) { + str <- '0.00' + } else if (abs(prob - 1) < 1e-3) { + str <- '1.00' + } else { + prob <- round(100 * prob) + if (prob < 10) { + str <- paste0('0.0', as.character(prob)) + } else { + str <- paste0('0.', as.character(prob)) + } + } + return(str) +} \ No newline at end of file diff --git a/TODO.md b/TODO.md index d8c50d2..5fee7dd 100644 --- a/TODO.md +++ b/TODO.md @@ -29,4 +29,8 @@ Function | Argument | Value | Matlab output | R output ---------|----------|-------|---------------|--------- `ownNum2Str` | `number` | `NaN` | `'NAN'` | error `ownNum2Str` | `number` | `` | `''` | `''` + warning -`repmat` | `length(n)` | `> 2` | > 2D matrix | 2D matrix \ No newline at end of file +`repmat` | `length(n)` | `> 2` | > 2D matrix | 2D matrix + +As general remarks, one should keep in mind that: + +- For compliance with IEC 60559, the `round` in base R rounds .5 to the nearest even integer, whereas the homonym function in Matlab rounds up (or down, if negative). \ No newline at end of file diff --git a/man/proportion2str.Rd b/man/proportion2str.Rd new file mode 100644 index 0000000..1263d77 --- /dev/null +++ b/man/proportion2str.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/proportion2str.R +\name{proportion2str} +\alias{proportion2str} +\title{Convert proportion to string} +\usage{ +proportion2str(prob) +} +\arguments{ +\item{prob}{belongs to [0.00, 0.01, ... ,1]} +} +\value{ +a 4-mark presentation of proportion +} +\description{ +Convert proportion to string +} +\note{ +The `round` function in R, being ISO-compliant, rounds 8.5 to 8. The +Matlab equivalent rounds it to 9. +} diff --git a/tests/testthat/test-admix1.R b/tests/testthat/test-admix1.R index af3ab1d..557db66 100644 --- a/tests/testthat/test-admix1.R +++ b/tests/testthat/test-admix1.R @@ -37,11 +37,17 @@ test_that("learn*partition behaves like on Matlab", { ) }) -test_that("ownNum2Str behaves like on Matlab", { +test_that("type convertions behave like on Matlab", { expect_equal(ownNum2Str(1), "1") expect_equal(ownNum2Str(-123456789), "-123456789") expect_equal(ownNum2Str(0), "0") expect_error(ownNum2Str("a")) + expect_equal(proportion2str(1), "1.00") + expect_equal(proportion2str(0), "0.00") + expect_equal(proportion2str(0.4), "0.40") + expect_equal(proportion2str(0.89), "0.89") + expect_equal(proportion2str(-0.4), "0.0-40") # also bugged in original + # TODO: fix after release, as long as it doesn't break anything else }) test_that("computeRows behaves like on Matlab", {