Skip to content
Snippets Groups Projects
Commit b4d33bbe authored by Todor Kondić's avatar Todor Kondić
Browse files

Function `gen_val_unc' now represents errors with uncertanties.

parent 63333fe7
No related branches found
No related tags found
No related merge requests found
......@@ -149,3 +149,23 @@ uniqy_slugs <- function(slugs) {
dt = data.table::data.table(slug=slugs)
dt[,slug:=fifelse(rep(.N==1L,.N),slug,paste0(slug,"_",seq(1L,.N))),by="slug"]$slug
}
gen_val_unc <- function(x,dx) {
## Doesn't work well for <=0.
p = floor(log10(x))
dp = floor(log10(dx))
## Zero?
message("p ",p)
w = which(is.infinite(p))
p[w] = 0
## Normalise x and dx.
main = x/10**p
unc = round(dx/10**dp,0)
place = p - dp
main = mapply(function (m,d) formatC(m,digits=d,format='f',flag="#"),main,place,USE.NAMES=F)
w = which(main=='10.')
main[w]='1'
p[w]=p[w]+1
paste0(main,"(",unc,") x 10^",p)
}
......@@ -15,3 +15,14 @@ test_that("uniqy_slugs",{
out = uniqy_slugs(inp)
expect_snapshot(out)
})
test_that("gen_val_unc",{
vals = c(1234567,991458,256.236546546,0.2,0.002,0)
dvals = c(6733,123000,1.e-4,0.5,0.0001,0.001)
x = gen_val_unc(vals,dvals)
message("\n")
message("====")
message(paste0(x,collapse=";;"))
print("----")
expect_true(1==1)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment