Skip to content

Commit

Permalink
Merge branch 'nicolas95870-develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
statnmap committed Jul 16, 2021
2 parents 13d65aa + 3ffebc8 commit 30f581e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ Depends:
Imports:
assertthat,
devtools,
dplyr (>= 0.8.0),
dplyr,
ggplot2,
lazyeval,
lubridate,
magrittr,
methods,
officer,
rvg,
stats,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ importFrom(ggplot2,is.ggplot)
importFrom(lazyeval,interp)
importFrom(lubridate,is.POSIXt)
importFrom(magrittr,"%>%")
importFrom(methods,is)
importFrom(officer,add_slide)
importFrom(officer,ph_location_type)
importFrom(officer,ph_with)
Expand Down
47 changes: 27 additions & 20 deletions R/as_numeric.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@

#' transform a vector into numeric
#' Transform a vector into numeric if meaningful, even with bad decimal, space or \%
#'
#' @param vec a vector
#'
#' @return a numeric vector
#' @details Note that text and factors are not transformed as numeric (except FALSE, TRUE, F, T),
#' contrary to R default behavior with `as.numeric(factor())`
#' @importFrom methods is
#' @export
#'

as_mon_numeric<-function(vec){
if (is.character(vec)){
vec<-as.factor(vec)
#' @examples
#' as_mon_numeric(c("1", "0", "1"))
#' as_mon_numeric(c("1.3", "1,5", "1;6", "16%", "17 87 "))
#' as_mon_numeric(c(TRUE, "A", "F"))
#' as_mon_numeric(c(TRUE, TRUE, FALSE))
#' as_mon_numeric(factor(c("toto", "tata", "toto")))
as_mon_numeric <- function(vec) {
assertthat::assert_that(not_empty(vec))
assertthat::assert_that("vector" %in% is(vec))

if (is.character(vec)) {
vec <- as.factor(vec)
}

if (is.factor(vec)){
levels(vec)<-gsub("^FALSE$",0,levels(vec))
levels(vec)<-gsub("^TRUE$",1,levels(vec))
levels(vec)<-gsub("^F$",0,levels(vec))
levels(vec)<-gsub("^T$",1,levels(vec))
if (is.factor(vec)) {
levels(vec) <- gsub("^FALSE$", 0, levels(vec))
levels(vec) <- gsub("^TRUE$", 1, levels(vec))
levels(vec) <- gsub("^F$", 0, levels(vec))
levels(vec) <- gsub("^T$", 1, levels(vec))
}

if (is.logical(vec)){
if (is.logical(vec)) {
return(as.numeric(vec))
}

out<-gsub("%","",vec)
out<-gsub(" ","",out)
out <-gsub(",",".",out)
out <-gsub(";",".",out)
out<-as.numeric(as.character(out))
out <- gsub("%", "", vec)
out <- gsub(" ", "", out)
out <- gsub(",", ".", out)
out <- gsub(";", ".", out)
out <- as.numeric(as.character(out))

return(out)
}


5 changes: 5 additions & 0 deletions devstuff_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,8 @@ usethis::pr_fetch(15)

# Development
devtools::check()

# test unitaire

usethis::use_test("as_numeric.R")
devtools::test()
15 changes: 13 additions & 2 deletions man/as_mon_numeric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-as_numeric.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("Output is a vector and numeric", {
expect_true(is.vector(as_mon_numeric(c(FALSE, 0, "0"))))
expect_true(is.numeric(as_mon_numeric(c(FALSE, 0, "0"))))
expect_true(is.numeric(as_mon_numeric(c("1.3", "1,5", "1;6", "16%", "17 87 "))))
expect_equal(as_mon_numeric(c("1.3", "1,5", "1;6", "16%", "17 87 ")),
c(1.3, 1.5, 1.6, 16, 1787))
expect_equal(as_mon_numeric(c(TRUE, "A", "F")), c(1, NA, 0))
expect_equal(as_mon_numeric(c(TRUE, T, F)), c(1, 1, 0))
expect_true(all(is.na(as_mon_numeric(factor(c("toto", "tata", "toto"))))))
})

test_that("Output and input has same length", {
expect_equal(length(c(FALSE, 0, "0")), length((as_mon_numeric(c(FALSE, 0, "0")))))
})

0 comments on commit 30f581e

Please sign in to comment.