-
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
Kevin Cazelles
committed
Jun 21, 2019
1 parent
16a2cae
commit f451ae6
Showing
67 changed files
with
1,206 additions
and
1,954 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,6 @@ LICENSE | |
record_updates.txt | ||
^docs$ | ||
^_pkgdown\.yml$ | ||
^doc$ | ||
^Meta$ | ||
^index.Rmd |
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 |
---|---|---|
|
@@ -37,3 +37,5 @@ vignettes/*.pdf | |
# Temporary files created by R markdown | ||
*.utf8.md | ||
*.knit.md | ||
doc | ||
Meta |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Apply a function on elements of a character string | ||
#' | ||
#' Apply a function on a given set of elements of a character string. | ||
#' | ||
#' @author | ||
#' Kevin Cazelles | ||
#' | ||
#' @param x a character vector, or a vector to be coerced to a character vector. | ||
#' @param FUN the function to be applied, see [base::lapply]. | ||
#' @param pos a vector indicating the elements position. | ||
#' @param pattern a see [base::gregexpr]. | ||
#' @param ... argument to be passed to `gregexpr`. | ||
#' | ||
#' @note | ||
#' In case both `pos` or `pattern`, the latter is ignored. | ||
#' | ||
#' @return | ||
#' A character vector. | ||
#' | ||
#' @importFrom magrittr %>% %<>% | ||
#' @export | ||
#' @examples | ||
#' applyString('cool', pos = 1:2, FUN = toupper) | ||
#' applyString(c('cool', 'pro'), pattern = 'o', FUN = toupper) | ||
|
||
|
||
applyString <- function(x, FUN, pos = NULL, pattern = NULL) { | ||
|
||
if (!is.character(x)) | ||
x <- as.character(x) | ||
|
||
if (!is.null(pos)) { | ||
tmp <- strsplit(x, split = "") | ||
tmp_fun <- function(x) { | ||
x[pos] %<>% FUN | ||
paste(x, collapse = "") | ||
} | ||
out <- lapply(tmp, tmp_fun) %>% unlist | ||
} else { | ||
if (is.null(pattern)) { | ||
warning("neither pos nor pattern is defined", call. = FALSE) | ||
out <- NULL | ||
} else { | ||
mat <- gregexpr(pattern = pattern, text = x) | ||
tmp_mth <- regmatches(x, mat) | ||
# NB: regmatches(x, mat, invert = TRUE) returns '' if first or last elements | ||
# match the pattern. Therefore there is alwasy 2*n - 1 number of elements in the | ||
# vector to be created (n being the size of tmp_inv elements). | ||
tmp_inv <- regmatches(x, mat, invert = TRUE) | ||
out <- apply(cbind(tmp_mth, tmp_inv), 1, FUN = reassemble, f = FUN) | ||
} | ||
} | ||
|
||
out | ||
} | ||
|
||
|
||
reassemble <- function(x, f) { | ||
char1 <- unlist(x[[1L]]) | ||
char2 <- unlist(x[[2L]]) | ||
sz <- length(char1) + length(char2) | ||
out <- rep("", sz) | ||
out[seq(1, sz, 2)] <- char2 | ||
if (sz > 1) | ||
out[seq(2, sz - 1, 2)] <- f(x[1L][[1L]]) | ||
paste(out, collapse = "") | ||
} |
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,35 @@ | ||
#' Assign a class to a set of column of a data frame | ||
#' | ||
#' This function is designed to ease the changes of column's class | ||
#' of a given data frame. | ||
#' | ||
#' @param x a data frame or a R object to be coerced into a data frame. | ||
#' @param colid the identity of columns for which class are ti be changed. | ||
#' @param cls a character vector containing the classes' names to be used in the | ||
#' same order as colid. By default \code{cls} is repeated until its size equals | ||
#' \code{colid}'s size. | ||
#' | ||
#' @return | ||
#' A data.frame for which columns have the required classes. | ||
#' | ||
#' @importFrom magrittr %<>% | ||
#' @export | ||
#' @examples | ||
#' df1 <- matrix(signif(runif(20),4), ncol=2) | ||
#' df2 <- assignClass2df(df1, 2, 'character') | ||
#' str(df1) | ||
#' str(df2) | ||
|
||
|
||
assignClass2df <- function(x, colid, cls) { | ||
## | ||
x %<>% as.data.frame | ||
stopifnot(colid %in% 1:ncol(x)) | ||
out <- x | ||
sz <- length(colid) | ||
cl <- rep(cls, length.out = sz) | ||
## | ||
for (i in 1:sz) out[, colid[i]] <- methods::as(x[, colid[i]], cl[i]) | ||
## | ||
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
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,23 @@ | ||
#' Compute the length of a strings. | ||
#' | ||
#' Compute the length of a vector of strings. | ||
#' | ||
#' @author | ||
#' Kevin Cazelles | ||
#' | ||
#' @param s character vector or a object to be coered as a character vector. | ||
#' @param ignore a vector of characters to be ignored in the count. | ||
#' | ||
#' @export | ||
#' | ||
#' @examples | ||
#' strLength(c(123)) | ||
#' strLength(c('/', 'four five'), ignore = c(" ", "f")) | ||
|
||
strLength <- function(s, ignore = "") { | ||
s %<>% as.character | ||
out <- strsplit(s, split = "") %>% | ||
lapply(function(x) sum(! x %in% ignore)) %>% | ||
unlist | ||
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.
Oops, something went wrong.