Skip to content

Instantly share code, notes, and snippets.

@eliardocosta
Last active July 28, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliardocosta/5937cc0bede30a3dff13192bc81b406b to your computer and use it in GitHub Desktop.
Save eliardocosta/5937cc0bede30a3dff13192bc81b406b to your computer and use it in GitHub Desktop.
# benford_file.R
#
# Copyright (c) 2017 Eliardo Costa
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the BSD Zero Clause License as published by
# Rob Landley. See http://spdx.org/licenses/0BSD.html for more details.
benford_file <- function(vecdata, filename, source.url, ds) {
cd <- getwd()
setwd(ds)
obj.vecdata <- benford(vecdata, 1)
sink(filename)
paste(cat(' { \n'),
cat(' "values": {\n'),
for (i in 1:9) {
cat(' ', paste('"',i,'": ', sep = ""),
round(100*as.numeric(obj.vecdata$bfd[i, 2]), 2), ifelse(i == 9, '\n', ',\n'), sep = "")
},
cat(' },\n'),
cat(' "num_records": ', '"', length(vecdata), '"', ',\n', sep = ""),
cat(' "min_value": ', '"', min(vecdata), '"', ',\n', sep = ""),
cat(' "max_value": ', '"', max(vecdata), '"', ',\n', sep = ""),
cat(' "source": ', '"', source.url, '"', '\n', sep = ""),
cat(' }\n')
)
sink()
setwd(cd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment