Skip to content

Commit

Permalink
Export methods correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
skhiggins committed May 7, 2018
1 parent 49adde1 commit 1cb756d
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 52 deletions.
7 changes: 7 additions & 0 deletions 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)
Expand Down
17 changes: 17 additions & 0 deletions R/quantiles.R
Expand Up @@ -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()
}
7 changes: 0 additions & 7 deletions R/quantiles.data.table.R

This file was deleted.

6 changes: 0 additions & 6 deletions R/quantiles.tbl_df.R

This file was deleted.

31 changes: 31 additions & 0 deletions R/tab.R
Expand Up @@ -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)
}

11 changes: 0 additions & 11 deletions R/tab.data.table.R

This file was deleted.

15 changes: 0 additions & 15 deletions R/tab.tbl_df.R

This file was deleted.

20 changes: 20 additions & 0 deletions R/tabcount.R
Expand Up @@ -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]]))
}

4 changes: 0 additions & 4 deletions R/tabcount.data.frame.R

This file was deleted.

4 changes: 0 additions & 4 deletions R/tabcount.data.table.R

This file was deleted.

5 changes: 0 additions & 5 deletions R/tabcount.tbl_df.R

This file was deleted.

0 comments on commit 1cb756d

Please sign in to comment.