From da921d274d752c62d21b30ae72dca4b736285eb4 Mon Sep 17 00:00:00 2001 From: Kanishka Narayan <37234086+kanishkan91@users.noreply.github.com> Date: Wed, 25 Mar 2020 19:13:30 -0400 Subject: [PATCH 1/2] Adding functions for fast_group_by and data_table_bind --- R/utils.R | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/R/utils.R b/R/utils.R index addba28aa..f1c53be37 100644 --- a/R/utils.R +++ b/R/utils.R @@ -494,3 +494,68 @@ screen_forbidden <- function(fn) { } rslt } + +#' fast_group_by +#' +#' A version of group_by that uses data.table instead of dplyr +#' +#' This group_by function that uses data.table offers a much higher speed +#' especially when working with high volume datasets. This function can also be +#' called within dplyr pipes. data.table will also inherently ensure consistency +#' between LHS and RHS +#' +#' +#' @param df The tibble on which the group_by is to be performed +#' @param by A vector with the criteria for the group_by. +#' @param colname A string with the column name on which the grouping is to be performed +#' @param func A string with the function to be performed. Default is set to "sum" +#' @return A tibble with the aggregated data. +#' @importFrom data.table as.data.table +#' @importFrom tibble as_tibble +#' @author kbn 24 Mar 2020 +fast_group_by<- function(df,by,colname="value",func= "sum"){ + + + #Convert relevant column to numeric + df[,colname]<- as.numeric(df[,colname]) + + #Store as data.table + df <- as.data.table(df) + + #Complete operations + df<- df[, (colname) := (get(func)(get(colname))), by] + + #Save back to tibble + df<- as_tibble(df) + + return(df) +} + +#' data_table_bind +#' +#' A binding function that uses data.table. This can be used as a replacement for rbind or bind_rows. +#' +#' This binding function takes advantage of the data processing capabilities of data.table. This can be +#' called within dplyr pipes. +#' +#' +#' @param ... The tibbles to be merged. +#' @return A tibble with the combined data. +#' @importFrom data.table as.data.table rbindlist +#' @importFrom tibble as_tibble +#' @return A tibble with combined data. +#' @author kbn 24 Mar 2020 +data_table_bind<-function(...){ + + #Create a list for binding + list_for_bind =list(...) + + #bind into one dataframe + df <- rbindlist(list_for_bind,use.names=TRUE) + + #Return as tibble + df<-as_tibble(df) + + return(df) + +} From 705e40d13941b976ff6845342eae0937334e9fad Mon Sep 17 00:00:00 2001 From: Kanishka Narayan <37234086+kanishkan91@users.noreply.github.com> Date: Thu, 26 Mar 2020 13:13:02 -0400 Subject: [PATCH 2/2] Commiting 2 functions with documentation 1. fast_group_by 2. data_table_bind --- R/utils.R | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/R/utils.R b/R/utils.R index f1c53be37..63d310eba 100644 --- a/R/utils.R +++ b/R/utils.R @@ -497,22 +497,30 @@ screen_forbidden <- function(fn) { #' fast_group_by #' -#' A version of group_by that uses data.table instead of dplyr +#' A version of group_by that uses data.table instead of dplyr. Creates groups, runs a user specified function, ungroups and returns +#' a processed tibble. Please use this function for grouping only numeric data. #' #' This group_by function that uses data.table offers a much higher speed #' especially when working with high volume datasets. This function can also be #' called within dplyr pipes. data.table will also inherently ensure consistency -#' between LHS and RHS +#' between LHS and RHS. The function will perform a combination of a group_by , mutate and ungroup. #' +#' Example- +#' +#' A group_by with dplyr - grouped_data <- data -> group_by(iso,year,glu_code) -> mutate(value=sum(value)) -> ungroup() +#' +#' Same group_by with data.table - grouped_data <- fast_group_by(data, by=c("iso","year","glu_code"),colname = "value", func = "sum" ) #' #' @param df The tibble on which the group_by is to be performed -#' @param by A vector with the criteria for the group_by. +#' @param by A vector of strings with the criteria for the group_by. #' @param colname A string with the column name on which the grouping is to be performed #' @param func A string with the function to be performed. Default is set to "sum" #' @return A tibble with the aggregated data. #' @importFrom data.table as.data.table #' @importFrom tibble as_tibble +#' @importFrom dplyr %>% #' @author kbn 24 Mar 2020 +#' @export fast_group_by<- function(df,by,colname="value",func= "sum"){ @@ -538,19 +546,24 @@ fast_group_by<- function(df,by,colname="value",func= "sum"){ #' This binding function takes advantage of the data processing capabilities of data.table. This can be #' called within dplyr pipes. #' +#'Example- +#' +#'Bind 2 datasets (x,y) with same columns using the following, +#' +#'Bound_dataset<- data_table_bind(x,y) #' #' @param ... The tibbles to be merged. -#' @return A tibble with the combined data. #' @importFrom data.table as.data.table rbindlist #' @importFrom tibble as_tibble #' @return A tibble with combined data. #' @author kbn 24 Mar 2020 +#' @export data_table_bind<-function(...){ #Create a list for binding list_for_bind =list(...) - #bind into one dataframe + #bind into one dataframe using rbindlist df <- rbindlist(list_for_bind,use.names=TRUE) #Return as tibble