Skip to content

Commit

Permalink
Merge pull request #9248 from lilyclements/n_distinct-fix
Browse files Browse the repository at this point in the history
Getting n_distinct working for summaries
  • Loading branch information
N-thony authored Nov 13, 2024
2 parents 9cd92ec + 36c517b commit dedb6c6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ DataBook$set("public", "apply_instat_calculation", function(calc, curr_data_list
# if it is a ordered factor...
if (any(stringr::str_detect("ordered", col_data_type))){
# put in here the ones that DO work for ordered factor
if (any(grepl("summary_count_non_missing|summary_count_missing|summary_count|summary_min|summary_max|summary_range|summary_median|summary_quantile|p10|p20|p25|p30|p33|p40|p60|p67|p70|p75|p80|p90", formula_fn_exp))){
if (any(grepl("summary_count_non_missing|summary_count_missing|summary_n_distinct|summary_count|summary_min|summary_max|summary_range|summary_median|summary_quantile|p10|p20|p25|p30|p33|p40|p60|p67|p70|p75|p80|p90", formula_fn_exp))){
curr_data_list[[c_data_label]] <- curr_data_list[[c_data_label]] %>%
dplyr::summarise(!!calc$result_name := !!rlang::parse_expr(calc$function_exp))
} else {
Expand All @@ -505,7 +505,7 @@ DataBook$set("public", "apply_instat_calculation", function(calc, curr_data_list
# if it is a factor or character, do not work for anything except...
} else if (any(stringr::str_detect("factor | character", col_data_type))){
# put in here the ones that DO work for factor or character
if (any(grepl("summary_count_non_missing|summary_count_missing|summary_count", formula_fn_exp))){
if (any(grepl("summary_count_non_missing|summary_count_missing|summary_n_distinct|summary_count", formula_fn_exp))){
curr_data_list[[c_data_label]] <- curr_data_list[[c_data_label]] %>%
dplyr::summarise(!!calc$result_name := !!rlang::parse_expr(calc$function_exp))
} else {
Expand Down Expand Up @@ -846,4 +846,4 @@ find_df_from_calc_from <- function(x, column) {
if(column %in% x[[i]]) return(names(x)[i])
}
return("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ summary_nth <- function(x, nth_value, order_by = NULL, ...) {

# n_distinct function
summary_n_distinct<- function(x, na.rm = FALSE, ...) {
return(dplyr::n_distinct(x = x, na.rm = na.rm))
return(dplyr::n_distinct(x, na.rm = na.rm))
}

# sample function
Expand Down

0 comments on commit dedb6c6

Please sign in to comment.