rsquaredacademy / descriptr
1
ds_freq_numeric <- function(data, variable, bins = 5) {
2

3 1
  check_df(data)
4 1
  var_name <- deparse(substitute(variable))
5 1
  varyable <- rlang::enquo(variable)
6 1
  check_numeric(data, !! varyable, var_name)
7

8 1
  fdata <-
9 1
    data %>%
10 1
    dplyr::pull(!! varyable) %>%
11 1
    na.omit()
12

13 1
  if (!is.numeric(bins)) {
14 0
    stop("bins must be integer value", call. = FALSE)
15
  }
16

17 1
  if (is.numeric(bins)) {
18 1
    bins <- as.integer(bins)
19
  }
20

21 1
  var_name <-
22 1
    data %>%
23 1
    dplyr::select(!! varyable) %>%
24 1
    names()
25

26 1
  n_bins   <- bins
27 1
  inta     <- intervals(fdata, bins)
28 1
  result   <- freq(fdata, bins, inta)
29 1
  data_len <- length(fdata)
30 1
  cum      <- cumsum(result)
31 1
  per      <- percent(result, data_len)
32 1
  cum_per  <- percent(cum, data_len)
33

34 1
  na_count <-
35 1
    data %>%
36 1
    dplyr::pull(!! varyable) %>%
37 1
    is.na() %>%
38 1
    sum()
39

40 1
  if (na_count > 0) {
41 0
    na_freq <- na_count
42
  } else {
43 1
    na_freq <- 0
44
  }
45

46 1
  n_obs <-
47 1
    data %>%
48 1
    dplyr::pull(!! varyable) %>%
49 1
    length()
50

51 1
  lower_n <- n_bins + 1
52

53 1
  freq_data <-
54 1
    tibble::tibble(lower        = inta[-lower_n],
55 1
                   upper        = inta[-1],
56 1
                   frequency    = result,
57 1
                   cumulative   = cum,
58 1
                   freq_percent = per,
59 1
                   cum_percent  = cum_per)
60

61 1
  utility <- list(breaks      = inta,
62 1
                  frequency   = result,
63 1
                  cumulative  = cum,
64 1
                  percent     = per,
65 1
                  cum_percent = cum_per,
66 1
                  bins        = n_bins,
67 1
                  data        = fdata,
68 1
                  na_count    = na_freq,
69 1
                  n           = n_obs,
70 1
                  varname     = var_name)
71

72 1
  out <- list(ftable  = freq_data,
73 1
              utility = utility)
74

75 1
  return(out)
76
}
77

78
plot_ds_freq_numeric <- function(x, ...) {
79

80 1
  x_lab <-
81 1
    x %>%
82 1
    use_series(utility) %>%
83 1
    use_series(varname)
84

85 1
  k <-
86 1
    x %>%
87 1
    use_series(utility) %>%
88 1
    use_series(varname) %>%
89 1
    extract(1) %>%
90 1
    rlang::sym()
91

92 1
  bins <-
93 1
    x %>%
94 1
    use_series(utility) %>%
95 1
    use_series(frequency) %>%
96 1
    length()
97

98 1
  p <-
99 1
    x %>%
100 1
    use_series(utility) %>%
101 1
    use_series(frequency) %>%
102 1
    tibble::enframe(name = NULL) %>%
103 1
    tibble::add_column(x = seq_len(x$utility$bins), .before = 1) %>%
104 1
    ggplot() +
105 1
    geom_col(
106 1
      aes(x = x, y = value), width = 0.999,
107 1
      fill = "blue", color = "black"
108
    ) +
109 1
    xlab(x_lab) + ylab("Count") +
110 1
    ggtitle(paste("Histogram of", x_lab))
111

112 1
  return(p)
113

114
}

Read our documentation on viewing source code .

Loading