-
Notifications
You must be signed in to change notification settings - Fork 0
/
ct.R
86 lines (67 loc) · 2.04 KB
/
ct.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#' An All-Purpose Count Function
#'
#' This function gives you a simple count with percentages that is weighted
#' @param df Name of the Dataset
#' @param var Variable to Count
#' @param wt Weighting Variable
#' @param show_na Will remove the NAs before the calculation if set to TRUE, defaults to FALSE
#' @param cum Will add a cumulative total if set to TRUE
#' @keywords Count
#' @export
#' @examples
#'
#'
#' cces <- read_csv("https://raw.githubusercontent.com/ryanburge/blocks/master/cces.csv")
#' cces %>%
#' ct(race)
#'
#'# With a weight
#' cces %>%
#' ct(race, wt = commonweight_vv)
#'
#'cces %>%
#' mutate(race2 = frcode(race == 1 ~ "White",
#' race == 2 ~ "Black",
#' race == 3 ~ "Hispanic",
#' race == 4 ~ "Asian")) %>%
#' ct(race2, show_na = FALSE, commonweight_vv)
#'
#'# Cumulative Counts
#'
#' cces %>%
#' ct(race, cum = TRUE)
#'
ct <- function(df, var, wt, cum = FALSE, show_na = TRUE) {
var <- enquo(var)
wt <- enquo(wt)
if(quo_is_missing(wt) && show_na == FALSE) {
df1 <- df %>%
dplyr::filter(!! var != "NA") %>%
dplyr::count(!! var) %>%
dplyr::mutate(pct = prop.table(n)) %>%
dplyr::mutate(pct = round(pct, 3))
} else if(quo_is_missing(wt) && show_na == TRUE){
df1 <- df %>%
dplyr::count(!! var) %>%
dplyr::mutate(pct = prop.table(n)) %>%
dplyr::mutate(pct = round(pct, 3))
} else if(show_na == FALSE){
df1 <- df %>%
dplyr::filter(!! var != "NA") %>%
dplyr::count(!! var, wt = !! wt) %>%
dplyr::mutate(pct = prop.table(n)) %>%
dplyr::mutate(pct = round(pct, 3))
} else {
df1 <- df %>%
dplyr::count(!! var, wt = !! wt) %>%
dplyr::mutate(pct = prop.table(n)) %>%
dplyr::mutate(pct = round(pct, 3))
}
if(cum == TRUE){
df1 %>%
dplyr::mutate(cum_n = cumsum(n)) %>%
dplyr::mutate(cum_pct = cumsum(pct))
} else {
df1
}
}