-
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
23 changed files
with
504 additions
and
83 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 |
---|---|---|
|
@@ -2,3 +2,7 @@ | |
.Rhistory | ||
.RData | ||
.Ruserdata | ||
*.xls | ||
*.xlsb | ||
*.xlsm | ||
*.xlsx |
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 |
---|---|---|
|
@@ -4,14 +4,16 @@ Version: 0.0.0.9000 | |
Authors@R: | ||
person(given = "seokhoon", family = "Joo", role = c("aut", "cre"), | ||
email = "[email protected]") | ||
Description: Joo's functional aid toolkit for efficient programming. | ||
Description: Joo's functional aid toolkit for efficient and productive programming. | ||
License: MIT + file LICENSE | ||
Encoding: UTF-8 | ||
Depends: R (>= 3.5.0) | ||
Imports: | ||
Rcpp (>= 0.12.10), | ||
data.table (>= 1.13.0), | ||
openssl (>= 1.4.1), | ||
openxlsx (>= 4.2.3) | ||
openxlsx (>= 4.2.3), | ||
tibble (>= 3.2.1) | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.2.3 | ||
LinkingTo: | ||
|
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 was deleted.
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,27 @@ | ||
#' loadRDS | ||
#' | ||
#' loadRDS is almost same as readRDS except for a pointer. | ||
#' | ||
#' @param file a \link{connection} or the name of the file where the R object is saved | ||
#' to or read from. | ||
#' @param refhook a hook function for handling reference objects. | ||
#' | ||
#' @return an \R object | ||
#' | ||
#' @examples | ||
#' # compare pointer values | ||
#' \dontrun{data <- copy(women) | ||
#' data.table::setDT(data) | ||
#' saveRDS(data, "data.rds") | ||
#' df <- readRDS("data.rds") | ||
#' dt <- loadRDS("data.rds") | ||
#' attributes(df)$.internal.selfref # <pointer: (nil)> | ||
#' attributes(dt)$.internal.selfref} | ||
#' | ||
#' @export | ||
loadRDS <- function(file, refhook = NULL) { | ||
df <- readRDS(file, refhook) | ||
if (inherits(df, "data.table")) | ||
return(alloc.col(df)) | ||
return(df) | ||
} |
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,29 +1,55 @@ | ||
|
||
# Calculate unique days in multiple overlapping date ranges | ||
# merge_date_range_overlap <- function(df, id_var, merge_var, from_var, to_var, interval = 0) { | ||
# id_var <- match_cols(df, vapply(substitute(id_var) , deparse, "character")) | ||
# merge_var <- match_cols(df, vapply(substitute(merge_var), deparse, "character")) | ||
# from_var <- match_cols(df, deparse(substitute(from_var))) | ||
# to_var <- match_cols(df, deparse(substitute(to_var))) | ||
# vars <- c(id_var, merge_var, from_var, to_var) | ||
# tmp <- df[, ..vars] | ||
# setnames(tmp, c(id_var, merge_var, "from", "to")) | ||
# setorderv(tmp, c(id_var, "from", "to")) | ||
# set(tmp, j = "sub_stay", value = 0) | ||
# ind <- .Call(IndexDateRangeOverlap, tmp[, ..id_var], | ||
# as_integer(tmp$from), | ||
# as_integer(tmp$to), | ||
# as_integer(interval)) | ||
# set(tmp, j = "loc", value = ind$loc) | ||
# set(tmp, j = "sub", value = ind$sub) | ||
# group <- c(id_var, "loc") | ||
# m <- tmp[, lapply(.SD, glue_code), keyby = group, .SDcols = merge_var] | ||
# s <- tmp[, .(from = min(from), to = max(to), sub_stay = sum(sub_stay) + sum(sub)), | ||
# keyby = group] | ||
# z <- m[s, on = group] | ||
# set(z, j = "loc", value = NULL) | ||
# set(z, j = "stay", value = as.numeric(z$to - z$from + 1 - z$sub_stay)) | ||
# set(z, j = "sub_stay", value = NULL) | ||
# setnames(z, c(vars, "stay")) | ||
# return(z) | ||
# } | ||
#' Combine overlapping date ranges | ||
#' | ||
#' Combine multiple overlapping date ranges. | ||
#' | ||
#' @param df a data.frame with data ranges | ||
#' @param id_var id column names | ||
#' @param merge_var column names to be collapsed | ||
#' @param from_var a column name of start date | ||
#' @param to_var a column name of end date | ||
#' @param interval an interval of previous end date to next start date to be combined | ||
#' @param collapse an optional character string to seperate the result of `merge_var` | ||
#' @return `data.frame` with no overlapping date ranges. | ||
#' | ||
#' @examples | ||
#' # combine overlapping date ranges | ||
#' id <- c("A", "A", "B") | ||
#' work <- c("cleansing", "analysis", "cleansing") | ||
#' sdate <- as.Date(c("2022-03-01", "2022-03-05", "2022-03-08")) | ||
#' edate <- as.Date(c("2022-03-06", "2022-03-09", "2022-03-10")) | ||
#' df <- data.frame(id = id, work = work, sdate = sdate, edate = edate) | ||
#' combine_overlapping_date_range(df, id, work, sdate, edate, interval = 0) | ||
#' | ||
#' @export | ||
combine_overlapping_date_range <- function(df, id_var, merge_var, from_var, to_var, | ||
interval = 0, collapse = "|") { | ||
old_class <- class(df) | ||
set_dt(df) | ||
id_var <- match_cols(df, vapply(substitute(id_var), deparse, "character")) | ||
merge_var <- match_cols(df, vapply(substitute(merge_var), deparse, "character")) | ||
from_var <- deparse(substitute(from_var)) | ||
to_var <- deparse(substitute(to_var)) | ||
all_var <- c(id_var, merge_var, from_var, to_var) | ||
dt <- df[, .SD, .SDcols = all_var] | ||
setnames(dt, c(id_var, merge_var, "from", "to")) | ||
setorderv(dt, c(id_var, "from", "to")) | ||
set(dt, j = "sub_stay", value = 0) | ||
index <- .Call(IndexOverlappingDateRange, dt[, .SD, .SDcols = id_var], | ||
dt$from, dt$to, interval = interval) | ||
set(dt, j = "loc", value = index$loc) # group index to combine | ||
set(dt, j = "sub", value = index$sub) # days to subtract, if the interval is longer than 0 | ||
group_var <- c(id_var, "loc") | ||
m <- dt[, lapply(.SD, function(x) paste0(unique(x[!is.na(x)]), collapse = collapse)), | ||
keyby = group_var, .SDcols = merge_var] | ||
from <- to <- sub_stay <- sub <- NULL | ||
s <- dt[, list(from = min(from), to = max(to), sub_stay = sum(sub_stay) + sum(sub)), | ||
keyby = group_var] | ||
z <- m[s, on = group_var] | ||
set(z, j = "loc", value = NULL) | ||
set(z, j = "stay", value = as.numeric(z$to - z$from + 1 - z$sub_stay)) | ||
set(z, j = "sub_stay", value = NULL) | ||
setnames(z, c(all_var, "stay")) | ||
setattr(z, "class", old_class) | ||
setattr(df, "class", old_class) | ||
return(z) | ||
} |
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,8 +1,119 @@ | ||
#' @title devars | ||
#' | ||
#' @description | ||
#' This function operates like `deparse(substitute(x))` inside the functions. | ||
#' | ||
#' @param x A string, vector or list expression that can be a string vector | ||
#' @return A string vector | ||
#' | ||
#' @examples | ||
#' # deparse(substitute(x)) | ||
#' \donttest{devars(expression) | ||
#' devars(c(expression, string)) | ||
#' devars(list(expression, string)) | ||
#' devars(.(expression, string))} | ||
#' | ||
#' @export | ||
devars <- function(x) { | ||
if (identical(parent.frame(), globalenv())) | ||
n <- sys.nframe() | ||
else n <- 1L | ||
x <- eval(substitute(substitute(x)), envir = parent.frame(n = max(n, 1L))) | ||
if (length(x) == 1L) | ||
return(deparse(x)) | ||
return(vapply(x, deparse, "character")[-1L]) | ||
} | ||
|
||
match_cols <- function(df, cols) names(df)[match(cols, names(df), 0L)] | ||
|
||
has_rows <- function(df) { | ||
df_name <- deparse(substitute(df)) | ||
if (!nrow(df)) { | ||
stop("'", df_name, "' doesn't have row(s): ", | ||
call. = FALSE) | ||
} | ||
} | ||
|
||
has_cols <- function(df, cols) { | ||
df_name <- deparse(substitute(df)) | ||
df_cols <- colnames(df) | ||
diff_cols <- setdiff(cols, df_cols) | ||
if (length(diff_cols) > 0) { | ||
stop("'", df_name, "' doesn't have column(s): ", | ||
paste0(diff_cols, collapse = ", "), ".", | ||
call. = FALSE) | ||
} | ||
} | ||
|
||
has_missing <- function(x) { | ||
column_name <- deparse(substitute(x)) | ||
if (any(is.na(x))) { | ||
stop("'", column_name, "' has missing value(s): ", | ||
call. = FALSE) | ||
} | ||
} | ||
|
||
sort_group_by <- function(x) { | ||
.Call(SortGroupBy, x) | ||
} | ||
|
||
match_cols <- function(df, cols) { | ||
colnames(df)[match(cols, colnames(df), 0L)] | ||
#' Paste vectors of a list | ||
#' | ||
#' Paste vectors of equal length in a list or data.frame | ||
#' | ||
#' @param x a list with same length vectors or data frame column vectors you want to paste. | ||
#' @param sep a character string to separate the terms. | ||
#' @return a vector pasted | ||
#' | ||
#' @examples | ||
#' # paste length and width of iris | ||
#' iris$size <- paste_list(iris[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")]) | ||
#' head(iris) | ||
#' | ||
#' @export | ||
paste_list <- function(x, sep = "|") { | ||
n <- length(x) | ||
if (n == 1L) { | ||
return(x[[1L]]) | ||
} else { | ||
return(do.call(function(...) paste(..., sep = sep), x)) | ||
} | ||
} | ||
|
||
#' Set data frame to tibble | ||
#' | ||
#' Set data frame to tibble class. | ||
#' | ||
#' @param x data.frame | ||
#' @return No return value. | ||
#' | ||
#' @examples | ||
#' # set data.frame to tibble | ||
#' \donttest{set_tibble(iris)} | ||
#' | ||
#' @export | ||
set_tibble <- function(x) { | ||
assert_class(x, "data.frame") | ||
if (!inherits(x, "tbl_df")) | ||
data.table::setattr(x, "class", c("tbl_df", "tbl", "data.frame")) | ||
invisible(x) | ||
} | ||
|
||
#' Set data.frame to data.table | ||
#' | ||
#' Set data.frame to data.table class. | ||
#' | ||
#' @param x data.frame | ||
#' @return No return value. | ||
#' | ||
#' @examples | ||
#' # set data.frame to data.table | ||
#' \donttest{set_dt(iris)} | ||
#' | ||
#' @export | ||
set_dt <- function(x) { | ||
assert_class(x, "data.frame") | ||
if (!inherits(x, "data.table")) | ||
data.table::setattr(x, "class", c("data.table", "data.frame")) | ||
invisible(x) | ||
} |
Oops, something went wrong.