forked from ThinkR-open/thinkr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))))) | ||
}) |