Skip to content

Commit

Permalink
added sort_factor()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWisemanKFI committed Oct 17, 2022
1 parent ff11260 commit 316e58b
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 111 deletions.
223 changes: 112 additions & 111 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,111 +1,112 @@
# Generated by roxygen2: do not edit by hand

export("%*=%")
export("%+%")
export("%+=%")
export("%-%")
export("%-=%")
export("%/=%")
export("%<=%")
export("%<~%")
export("%==%")
export("%===%")
export("%><%")
export("%>=%")
export("%>=<%")
export("%>~%")
export("%^=%")
export("%aon%")
export("%log=%")
export("%na<-%")
export("%ni%")
export("%perl%")
export("%regex<-%")
export("%regex=%")
export("%rlike%")
export("%root=%")
export("%s*%")
export("%s/%")
export("%xor%")
export("%~=%")
export(IQR_cc)
export(all_cc)
export(all_good_for_calcs)
export(any_bad_for_calcs)
export(any_cc)
export(as.class)
export(bool)
export(cat0)
export(catN)
export(check_ext_against)
export(chr)
export(colMeans_cc)
export(colSums_cc)
export(cor_cc)
export(cov_cc)
export(dbl)
export(f.as.numeric)
export(get_1st)
export(get_1st_word)
export(get_last)
export(get_last_word)
export(get_most_frequent)
export(get_most_frequent_word)
export(get_nth)
export(get_nth_word)
export(get_os)
export(int)
export(is.RStudio)
export(is.R_revo)
export(is.R_x64)
export(is.atomic_nan)
export(is.bad_and_equal)
export(is.bad_for_calcs)
export(is.bad_for_indexing)
export(is.binary)
export(is.character_or_null)
export(is.constant)
export(is.df_or_null)
export(is.good_for_calcs)
export(is.good_for_indexing)
export(is.irregular_list)
export(is.list_or_null)
export(is.logical_or_null)
export(is.null_or_na)
export(is.numeric_or_null)
export(is.os_lnx)
export(is.os_mac)
export(is.os_unx)
export(is.os_win)
export(is.os_x64)
export(is.scalar)
export(is.scalar_or_null)
export(is_csv_file)
export(is_excel_file)
export(is_r_file)
export(is_rdata_file)
export(is_spss_file)
export(is_txt_file)
export(length_cc)
export(mad_cc)
export(max_cc)
export(mean_cc)
export(median_cc)
export(min_cc)
export(n_unique)
export(num)
export(paste_)
export(paste_oxford)
export(paste_series)
export(prod_cc)
export(quantile_cc)
export(range_cc)
export(read.psv)
export(read.tsv)
export(rowMeans_cc)
export(rowSums_cc)
export(sd_cc)
export(sum_cc)
export(var_cc)
export(weighted.mean_cc)
import(stats)
# Generated by roxygen2: do not edit by hand

export("%*=%")
export("%+%")
export("%+=%")
export("%-%")
export("%-=%")
export("%/=%")
export("%<=%")
export("%<~%")
export("%==%")
export("%===%")
export("%><%")
export("%>=%")
export("%>=<%")
export("%>~%")
export("%^=%")
export("%aon%")
export("%log=%")
export("%na<-%")
export("%ni%")
export("%perl%")
export("%regex<-%")
export("%regex=%")
export("%rlike%")
export("%root=%")
export("%s*%")
export("%s/%")
export("%xor%")
export("%~=%")
export(IQR_cc)
export(all_cc)
export(all_good_for_calcs)
export(any_bad_for_calcs)
export(any_cc)
export(as.class)
export(bool)
export(cat0)
export(catN)
export(check_ext_against)
export(chr)
export(colMeans_cc)
export(colSums_cc)
export(cor_cc)
export(cov_cc)
export(dbl)
export(f.as.numeric)
export(get_1st)
export(get_1st_word)
export(get_last)
export(get_last_word)
export(get_most_frequent)
export(get_most_frequent_word)
export(get_nth)
export(get_nth_word)
export(get_os)
export(int)
export(is.RStudio)
export(is.R_revo)
export(is.R_x64)
export(is.atomic_nan)
export(is.bad_and_equal)
export(is.bad_for_calcs)
export(is.bad_for_indexing)
export(is.binary)
export(is.character_or_null)
export(is.constant)
export(is.df_or_null)
export(is.good_for_calcs)
export(is.good_for_indexing)
export(is.irregular_list)
export(is.list_or_null)
export(is.logical_or_null)
export(is.null_or_na)
export(is.numeric_or_null)
export(is.os_lnx)
export(is.os_mac)
export(is.os_unx)
export(is.os_win)
export(is.os_x64)
export(is.scalar)
export(is.scalar_or_null)
export(is_csv_file)
export(is_excel_file)
export(is_r_file)
export(is_rdata_file)
export(is_spss_file)
export(is_txt_file)
export(length_cc)
export(mad_cc)
export(max_cc)
export(mean_cc)
export(median_cc)
export(min_cc)
export(n_unique)
export(num)
export(paste_)
export(paste_oxford)
export(paste_series)
export(prod_cc)
export(quantile_cc)
export(range_cc)
export(read.psv)
export(read.tsv)
export(rowMeans_cc)
export(rowSums_cc)
export(sd_cc)
export(sort_factor)
export(sum_cc)
export(var_cc)
export(weighted.mean_cc)
import(stats)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# roperators 1.2.1

* added `sort_factor()`

# roperators 1.2.0.9000

* Cleaned up a lot of the code to be standardized and added additional examples
Expand Down
24 changes: 24 additions & 0 deletions R/shorthand.R
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,30 @@ get_most_frequent_word <- function(x,
return(out)
}

#' Sort a factor with optional reference level
#' @param x A vector to be turned into a sorted factor
#' @param ref Character - optional level to be set first
#' @param decreasing Logical - sort resulting factor in decreasing order
#' @param force_ref Logical - force ref level even if `ref` is not in the data
#' @rdname sort_factor
#' @export
sort_factor <- function(x, ref = NULL, decreasing = FALSE, force_ref = FALSE){
# check if ref exists in x.
# skip if force_ref is TRUE -> allow user to make factor level with 0 observations
if(!force_ref){
if(length(ref) && !ref %in% x) ref <- NULL # make NULL if it doesn't exist
}
# Will make this a pipe when more people have R >= 4.1
lbl <- factor(x)
lbl <- summary(lbl)
lbl <- sort(lbl, decreasing = decreasing)
lbl <- names(lbl)
lbl <- c(ref, lbl)
out <- factor(x, levels = lbl, labels=lbl)
return(out)
}


#' Return Number of Unique Things
#'
#' @param x vector
Expand Down

0 comments on commit 316e58b

Please sign in to comment.