R/internal.functions.R
changed.
Other files ignored by Codecov
man/truncDec.Rd
is new.
tests/testthat/test-extract.R
has changed.
NAMESPACE
has changed.
man/last.two.digits.Rd
has changed.
191 | 191 | return(results) |
|
192 | 192 | } |
|
193 | 193 | ||
194 | + | #' @title Trunc decimal values. |
|
195 | + | #' @description It trunc decimal values from the data. |
|
196 | + | #' |
|
197 | + | #'This function is used by the main function of the package \code{\link{benford}} |
|
198 | + | #' to trunc the values in its ndec argument to the specified number of decimal places. |
|
199 | + | #' |
|
200 | + | #' @param x a numeric vector. |
|
201 | + | #' @param n.dec integer indicating the number of decimal places (trunc) to be used. |
|
202 | + | #' @return |
|
203 | + | #' @export |
|
204 | + | ||
205 | + | truncDec <- function(x, n.dec = 2){ |
|
206 | + | int <- floor(x) |
|
207 | + | isDec <- grepl("\\.", as.character(x)) |
|
208 | + | dec <- sub("^[0-9]*\\.", "", as.character(x)) |
|
209 | + | truncateDec <- substr(dec, 1, n.dec) |
|
210 | + | truncatedNumber <- vector("character", length = length(dec)) |
|
211 | + | truncatedNumber[isDec] <- paste0(int[isDec], ".", dec[isDec]) |
|
212 | + | truncatedNumber[!isDec] <- as.character(int[!isDec]) |
|
213 | + | truncatedNumber <- as.numeric(truncatedNumber) |
|
214 | + | return(truncatedNumber) |
|
215 | + | } |
|
194 | 216 | ||
195 | 217 | ||
196 | 218 | #' @title Extracts the last two digits from the data |
200 | 222 | #'ast two digits of the data. |
|
201 | 223 | #' |
|
202 | 224 | #' @param data a numeric vector. |
|
203 | - | #' @param sign The default value for sign is "positive" and it analyzes only data greater than zero. |
|
225 | + | #' @param ndec it specifies the number of decimals to be used (default 2). |
|
226 | + | #' @param sign The default value for sign is "positive" and it analyzes only data greater than zero. |
|
204 | 227 | #' There are also the options "negative" and "both" that will analyze only negative values or both positive and negative values of the data, |
|
205 | 228 | #' respectively. For large datasets with both positive and negative numbers, |
|
206 | 229 | #' it is usually recommended to perform a separate analysis for each group, |
|
207 | 230 | #' for the incentives to manipulate the numbers are usually different. |
|
208 | 231 | #' @return A data.frame with the data and the last digits. |
|
209 | 232 | #' @export |
|
210 | - | last.two.digits <- function(data, round = 2, sign="positive") { |
|
233 | + | ||
234 | + | last.two.digits <- function(data, sign="positive", ndec = 2) { |
|
211 | 235 | ||
212 | 236 | if (!is.numeric(data)) stop("Data must be a numeric vector") |
|
213 | 237 |
216 | 240 | if (sign == "negative") positives <- data[data < 0 & !is.na(data)]*(-1) |
|
217 | 241 | if (sign == "both") positives <- abs(data[data != 0 & !is.na(data)]) |
|
218 | 242 | ||
219 | - | digits.as.str <- as.character(positives) |
|
220 | - | nozeros <- grepl("\\.", digits.as.str) |
|
221 | - | digits.as.str.nz <- digits.as.str[nozeros] |
|
222 | - | dgts.after.dot <- sub("^[0-9]*\\.", "", digits.as.str.nz) |
|
223 | - | ltd.dgts.after.dot <- substr(dgts.after.dot, nchar(dgts.after.dot) - 1, nchar(dgts.after.dot)) |
|
224 | - | which.dgts.mult.10 <- grepl("^0", ltd.dgts.after.dot) |
|
225 | - | dgts.mult.10 <- as.character(as.numeric(ltd.dgts.after.dot[which.dgts.mult.10])*10) |
|
226 | - | ltd.dgts.after.dot[which.dgts.mult.10] <- dgts.mult.10 |
|
227 | - | digits.as.str[!nozeros] <- 0 |
|
228 | - | digits.as.str[nozeros] <- ltd.dgts.after.dot |
|
229 | - | ltd <- as.numeric(digits.as.str) |
|
243 | + | remainder <- positives %% 1 |
|
244 | + | if (all(remainder == 0)){ |
|
245 | + | warning("Data appears to be integers, so the argument 'ndec' is set to zero.") |
|
246 | + | ndec <- 0 |
|
247 | + | } |
|
230 | 248 | ||
231 | - | results <- data.frame(data = positives, |
|
249 | + | truncated.values <- truncDec(positives, ndec) |
|
250 | + | values.as.str <- as.character(truncated.values) |
|
251 | + | if (ndec == 0){ |
|
252 | + | nchar.values <- nchar(truncated.values) |
|
253 | + | ltd <- substr(truncated.values, nchar.values - 1, nchar.values) |
|
254 | + | ltd <- as.numeric(ltd) |
|
255 | + | }else{ |
|
256 | + | nozeros <- grepl("\\.", values.as.str) |
|
257 | + | values.as.str.nz <- values.as.str[nozeros] |
|
258 | + | dgts.after.dot <- sub("^[0-9]*\\.", "", values.as.str.nz) |
|
259 | + | ltd.dgts.after.dot <- substr(dgts.after.dot, nchar(dgts.after.dot) - 1, nchar(dgts.after.dot)) |
|
260 | + | which.dgts.mult.10 <- nchar(ltd.dgts.after.dot) == 1 |
|
261 | + | dgts.mult.10 <- as.character(as.numeric(ltd.dgts.after.dot[which.dgts.mult.10])*10) |
|
262 | + | ltd.dgts.after.dot[which.dgts.mult.10] <- dgts.mult.10 |
|
263 | + | values.as.str[!nozeros] <- 0 |
|
264 | + | values.as.str[nozeros] <- ltd.dgts.after.dot |
|
265 | + | ltd <- as.numeric(values.as.str) |
|
266 | + | } |
|
267 | + | ||
268 | + | results <- data.frame(data = truncated.values, |
|
232 | 269 | data.digits = ltd) |
|
233 | 270 | return(results) |
|
234 | 271 | } |
Files | Coverage |
---|---|
R | 99.51% |
Project Totals (4 files) | 99.51% |