Skip to content

Commit

Permalink
added interpolate function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewallenbruce committed Nov 18, 2024
1 parent c0e7ddd commit b080ff1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Imports:
pins,
purrr,
rlang,
stats,
strex,
stringfish,
stringi,
Expand All @@ -46,7 +47,6 @@ Suggests:
prettycode,
roxyglobals,
scales,
stats,
testthat (>= 3.0.0),
timeplyr,
wakefield
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export(gluestick)
export(gt_marks)
export(histo)
export(histogram)
export(interpolate)
export(invert_named)
export(is_valid_npi)
export(list_pins)
Expand Down
66 changes: 66 additions & 0 deletions R/generated-globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ utils::globalVariables(c(
# <change_lagged>
# <rate_of_return>
":=",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"a1",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"a2",
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"a3",
# <group_hcpcs_4>
# <group_hcpcs_5>
"a4",
# <group_hcpcs_5>
"a5",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"code",
# <combine>
"combined",
# <rate_of_return>
Expand All @@ -22,10 +46,43 @@ utils::globalVariables(c(
"date_of_submission",
# <forager_data>
"dates",
# <process_families>
"families",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"g",
# <id_runs>
"group",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"group_id",
# <id_runs>
"group_size",
# <process_groups>
"groups",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"i1",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"i2",
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"i3",
# <group_hcpcs_4>
# <group_hcpcs_5>
"i4",
# <group_hcpcs_5>
"i5",
# <make_interval>
"interval",
# <describe>
Expand All @@ -42,7 +99,16 @@ utils::globalVariables(c(
# <count_prop>
# <count_prop_multi>
# <count_wide>
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"n",
# <group_hcpcs_2>
# <group_hcpcs_3>
# <group_hcpcs_4>
# <group_hcpcs_5>
"n1",
# <describe>
"nuniq",
# <describe>
Expand Down
49 changes: 49 additions & 0 deletions R/programming.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,52 @@ srchcol <- function(df,
)
)
}

#' Linearly interpolate the values between two points
#'
#' [SO Link](https://stackoverflow.com/questions/27920690/linear-interpolation-using-dplyr/31845035#31845035)
#'
#' @param x,y `<numeric>` vectors giving the coordinates of the points to be
#' interpolated
#'
#' @param method `<character>` interpolation method, default is `"approx"`
#'
#' @returns `<numeric>` vector of interpolated values
#'
#' @examples
#' interpolate(1:5, c(10, NA, NA, NA, 100), "spline")
#'
#' df <- dplyr::tibble(
#' seq = 1:5,
#' v1 = c(1, NA, 3, NA, 5),
#' v2 = c(40, NA, 60, NA, 70),
#' v3 = c(10, NA, NA, NA, 100))
#'
#' df
#'
#' df |>
#' dplyr::mutate(
#' dplyr::across(
#' .cols = dplyr::starts_with("v"),
#' .fns = ~ interpolate(seq, .x),
#' .names = "{.col}_est"))
#'
#' df |>
#' dplyr::mutate(
#' dplyr::across(
#' dplyr::starts_with("v"),
#' ~ interpolate(seq, .x)))
#'
#' @autoglobal
#'
#' @export
interpolate <- function(x, y, method = c("approx", "spline")) {

method <- match.arg(method)

switch (
method,
approx = stats::approx(x, y, n = length(x))$y,
spline = stats::spline(x, y, n = length(x))$y
)
}
8 changes: 8 additions & 0 deletions R/regex_hcpcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ group_split_length <- function(x) {
}

#' @noRd
#' @autoglobal
group_hcpcs_1 <- function(x) {

if (!rlang::has_name(x, "x1")) return(NULL)
Expand All @@ -185,6 +186,7 @@ group_hcpcs_1 <- function(x) {
}

#' @noRd
#' @autoglobal
group_hcpcs_2 <- function(x) {

if (!rlang::has_name(x, "x2")) return(NULL)
Expand Down Expand Up @@ -227,7 +229,9 @@ group_hcpcs_2 <- function(x) {
)
grouped[collapse::radixorderv(collapse::vlengths(grouped), sort = TRUE)]
}

#' @noRd
#' @autoglobal
group_hcpcs_3 <- function(x) {

if (!rlang::has_name(x, "x3")) return(NULL)
Expand Down Expand Up @@ -273,6 +277,7 @@ group_hcpcs_3 <- function(x) {
grouped[collapse::radixorderv(collapse::vlengths(grouped), sort = TRUE)]
}
#' @noRd
#' @autoglobal
group_hcpcs_4 <- function(x) {

if (!rlang::has_name(x, "x4")) return(NULL)
Expand Down Expand Up @@ -320,6 +325,7 @@ group_hcpcs_4 <- function(x) {
grouped[collapse::radixorderv(collapse::vlengths(grouped), sort = TRUE)]
}
#' @noRd
#' @autoglobal
group_hcpcs_5 <- function(x) {

if (!rlang::has_name(x, "x5")) return(NULL)
Expand Down Expand Up @@ -372,6 +378,7 @@ group_hcpcs_5 <- function(x) {
}

#' @noRd
#' @autoglobal
process_groups <- function(x) {
list(
g1 = group_hcpcs_1(groups),
Expand Down Expand Up @@ -473,6 +480,7 @@ process_hcpcs_2 <- function(x) {
fuimus::collapser(to_vec)
}
#' @noRd
#' @autoglobal
process_families <- function(x) {

x <- list(
Expand Down
45 changes: 45 additions & 0 deletions man/interpolate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b080ff1

Please sign in to comment.