From 1cb756d49e60237ded61876bf54e5d609229c102 Mon Sep 17 00:00:00 2001 From: skhiggins Date: Sun, 6 May 2018 17:34:06 -0700 Subject: [PATCH] Export methods correctly --- NAMESPACE | 7 +++++++ R/quantiles.R | 17 +++++++++++++++++ R/quantiles.data.table.R | 7 ------- R/quantiles.tbl_df.R | 6 ------ R/tab.R | 31 +++++++++++++++++++++++++++++++ R/tab.data.table.R | 11 ----------- R/tab.tbl_df.R | 15 --------------- R/tabcount.R | 20 ++++++++++++++++++++ R/tabcount.data.frame.R | 4 ---- R/tabcount.data.table.R | 4 ---- R/tabcount.tbl_df.R | 5 ----- 11 files changed, 75 insertions(+), 52 deletions(-) delete mode 100644 R/quantiles.data.table.R delete mode 100644 R/quantiles.tbl_df.R delete mode 100644 R/tab.data.table.R delete mode 100644 R/tab.tbl_df.R delete mode 100644 R/tabcount.data.frame.R delete mode 100644 R/tabcount.data.table.R delete mode 100644 R/tabcount.tbl_df.R diff --git a/NAMESPACE b/NAMESPACE index d1b5e68..23e4812 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,12 @@ # Generated by roxygen2: do not edit by hand +S3method(quantiles,data.table) +S3method(quantiles,tbl_df) +S3method(tab,data.table) +S3method(tab,tbl_df) +S3method(tabcount,data.frame) +S3method(tabcount,data.table) +S3method(tabcount,tbl_df) export(quantiles) export(quo_to_chr) export(tab) diff --git a/R/quantiles.R b/R/quantiles.R index 5c6e2ed..771f84b 100644 --- a/R/quantiles.R +++ b/R/quantiles.R @@ -25,3 +25,20 @@ quantiles <- function(df, ...) { UseMethod("quantiles", df) # to do: add round option like in tab() } + +#' @export +quantiles.data.table <- function(df, ..., probs = seq(0, 1, 0.1), na.rm = FALSE) { + vars <- dplyr::quos(...) %>% quo_to_chr() + tabbed <- df[, lapply(.SD, function(x) quantile(x, probs = probs, na.rm = na.rm)), + .SDcols = vars + ][, p := probs] %>% setcolorder(c("p", vars)) + tabbed[] # make sure it prints +} + +#' @export +quantiles.tbl_df <- function(df, ..., probs = seq(0, 1, 0.1), na.rm = FALSE) { + vars <- dplyr::quos(...) + df %>% + dplyr::summarize(p = list(probs), q = list(quantile(!!!vars, probs))) %>% + tidyr::unnest() +} diff --git a/R/quantiles.data.table.R b/R/quantiles.data.table.R deleted file mode 100644 index a64f8e0..0000000 --- a/R/quantiles.data.table.R +++ /dev/null @@ -1,7 +0,0 @@ -quantiles.data.table <- function(df, ..., probs = seq(0, 1, 0.1), na.rm = FALSE) { - vars <- dplyr::quos(...) %>% quo_to_chr() - tabbed <- df[, lapply(.SD, function(x) quantile(x, probs = probs, na.rm = na.rm)), - .SDcols = vars - ][, p := probs] %>% setcolorder(c("p", vars)) - tabbed[] # make sure it prints -} diff --git a/R/quantiles.tbl_df.R b/R/quantiles.tbl_df.R deleted file mode 100644 index ac5a9c8..0000000 --- a/R/quantiles.tbl_df.R +++ /dev/null @@ -1,6 +0,0 @@ -quantiles.tbl_df <- function(df, ..., probs = seq(0, 1, 0.1), na.rm = FALSE) { - vars <- dplyr::quos(...) - df %>% - dplyr::summarize(p = list(probs), q = list(quantile(!!!vars, probs))) %>% - tidyr::unnest() -} diff --git a/R/tab.R b/R/tab.R index 092bcdb..c9262ac 100644 --- a/R/tab.R +++ b/R/tab.R @@ -28,3 +28,34 @@ tab <- function(df, ...) { UseMethod("tab", df) } + +#' @export +tab.data.table <- function(df, ..., round=2) { # note ... is the variable names to group by + group_by <- dplyr::quos(...) %>% quo_to_chr() + rowsofdata <- df[, .N] # faster than nrow() on big data.tables + df[, .N, by = group_by][, + temp_prop := N/rowsofdata][, + prop := round(temp_prop, digits = round)][ + order(-N)][, # sort in descending order by N before cumulative prop + cum_prop := round(cumsum(temp_prop), digits = round)][, + temp_prop := NULL][ # remove temp var + order(-N)] # make sure final data.table sorted +} + +#' @export +tab.tbl_df <- function(df, ..., round = 2) { # to check without requiring tibble + group_by <- dplyr::quos(...) + rowsofdata <- nrow(df) + df %>% + dplyr::group_by(!!!group_by) %>% # !!! since it's a quosure + dplyr::summarize(N = n()) %>% + dplyr::arrange(desc(N)) %>% + dplyr::ungroup() %>% + dplyr::mutate( + temp_prop = N/rowsofdata, + prop = round(temp_prop, digits = round), + cum_prop = round(cumsum(temp_prop), digits = round) + ) %>% + dplyr::select(-temp_prop) +} + diff --git a/R/tab.data.table.R b/R/tab.data.table.R deleted file mode 100644 index b8fc031..0000000 --- a/R/tab.data.table.R +++ /dev/null @@ -1,11 +0,0 @@ -tab.data.table <- function(df, ..., round=2) { # note ... is the variable names to group by - group_by <- dplyr::quos(...) %>% quo_to_chr() - rowsofdata <- df[, .N] # faster than nrow() on big data.tables - df[, .N, by = group_by][, - temp_prop := N/rowsofdata][, - prop := round(temp_prop, digits = round)][ - order(-N)][, # sort in descending order by N before cumulative prop - cum_prop := round(cumsum(temp_prop), digits = round)][, - temp_prop := NULL][ # remove temp var - order(-N)] # make sure final data.table sorted -} diff --git a/R/tab.tbl_df.R b/R/tab.tbl_df.R deleted file mode 100644 index a442452..0000000 --- a/R/tab.tbl_df.R +++ /dev/null @@ -1,15 +0,0 @@ -tab.tbl_df <- function(df, ..., round = 2) { # to check without requiring tibble - group_by <- dplyr::quos(...) - rowsofdata <- nrow(df) - df %>% - dplyr::group_by(!!!group_by) %>% # !!! since it's a quosure - dplyr::summarize(N = n()) %>% - dplyr::arrange(desc(N)) %>% - dplyr::ungroup() %>% - dplyr::mutate( - temp_prop = N/rowsofdata, - prop = round(temp_prop, digits = round), - cum_prop = round(cumsum(temp_prop), digits = round) - ) %>% - dplyr::select(-temp_prop) -} diff --git a/R/tabcount.R b/R/tabcount.R index edc0467..9d17a95 100644 --- a/R/tabcount.R +++ b/R/tabcount.R @@ -27,3 +27,23 @@ tabcount <- function(df, ...) { # note ... is the variable names to group by UseMethod("tabcount", df) } + +#' @export +tabcount.data.table <- function(df, ...) { + group_by <- dplyr::quos(...) %>% quo_to_chr() + df[, .N, by = group_by][, .N] +} + +#' @export +tabcount.tbl_df <- function(df, ...) { + group_by <- dplyr::quos(...) + df %>% + dplyr::n_distinct(!!!group_by) # !!! since it's a quosure +} + +#' @export +tabcount.data.frame <- function(df, ...) { + group_by <- quo_to_chr(group_by) + length(unique(df[[group_by]])) +} + diff --git a/R/tabcount.data.frame.R b/R/tabcount.data.frame.R deleted file mode 100644 index 51f7dc3..0000000 --- a/R/tabcount.data.frame.R +++ /dev/null @@ -1,4 +0,0 @@ -tabcount.data.frame <- function(df, ...) { - group_by <- quo_to_chr(group_by) - length(unique(df[[group_by]])) -} diff --git a/R/tabcount.data.table.R b/R/tabcount.data.table.R deleted file mode 100644 index 9238592..0000000 --- a/R/tabcount.data.table.R +++ /dev/null @@ -1,4 +0,0 @@ -tabcount.data.table <- function(df, ...) { - group_by <- dplyr::quos(...) %>% quo_to_chr() - df[, .N, by = group_by][, .N] -} diff --git a/R/tabcount.tbl_df.R b/R/tabcount.tbl_df.R deleted file mode 100644 index db98c74..0000000 --- a/R/tabcount.tbl_df.R +++ /dev/null @@ -1,5 +0,0 @@ -tabcount.tbl_df <- function(df, ...) { - group_by <- dplyr::quos(...) - df %>% - dplyr::n_distinct(!!!group_by) # !!! since it's a quosure -}