Skip to content

Commit

Permalink
Merge pull request #52 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
spsanderson authored Jul 29, 2024
2 parents 8ada13e + bc53a5b commit a62510d
Show file tree
Hide file tree
Showing 35 changed files with 316 additions and 26 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(csd)
export(cskewness)
export(cvar)
export(discrete_walk)
export(euclidean_distance)
export(geometric_brownian_motion)
export(kurtosis_vec)
export(rand_walk_helper)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ None
7. Fix #43 - Add vectorized functions
8. Fix #44 - Add Function `internal_rand_walk_helper()` to help generate common
columns for random walks.
9. Fix #34 - Add Function `euclidean_distance()` to calculate the Euclidean distance
of a random walk.

## Minor Improvements and Fixes
None
File renamed without changes.
56 changes: 56 additions & 0 deletions R/vec-distance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Distance Calculations
#'
#' @family Vector Function
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details
#' A function to calculate the Euclidean distance between two vectors. It uses
#' the formula `sqrt((x - lag(x))^2 + (y - lag(y))^2)`. The function uses augments
#' the data frame with a new column called `distance`.
#'
#' @description
#' A function to calculate the Euclidean distance between two vectors.
#'
#' @param .data A data frame
#' @param .x A numeric vector
#' @param .y A numeric vector
#' @param .pull_vector A boolean of TRUE or FALSE. Default is FALSE which will
#' augment the distance to the data frame. TRUE will return a vector of the distances
#' as the return.
#'
#' @examples
#' set.seed(123)
#' df <- rw30()
#' euclidean_distance(df, x, y)
#' euclidean_distance(df, x, y, TRUE) |> head(10)
#'
#' @return
#' A numeric Vector of ditances
#'
#' @name euclidean_distance
NULL
#' @rdname euclidean_distance
#' @export

euclidean_distance <- function(.data, .x, .y, .pull_vector = FALSE) {

# Tidyeval ---
x_var <- rlang::enquo(.x)
y_var <- rlang::enquo(.y)

# Calculate the distance ---
ret <- dplyr::as_tibble(.data) |>
dplyr::mutate(
distance = sqrt(
(!!x_var - dplyr::lag(!!x_var, 1))^2 + (!!y_var - dplyr::lag(!!y_var, 1))^2
)
)

# Return the distance vector ---
if (.pull_vector) {
return(ret[["distance"]])
}

return(ret)
}
1 change: 1 addition & 0 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pkgdown: 2.1.0
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2024-07-29T19:09Z
last_built: 2024-07-29T20:03Z
urls:
reference: https://www.spsanderson.com/RandomWalker/reference
article: https://www.spsanderson.com/RandomWalker/articles
3 changes: 2 additions & 1 deletion docs/reference/cgmean.html

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

3 changes: 2 additions & 1 deletion docs/reference/chmean.html

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

3 changes: 2 additions & 1 deletion docs/reference/ckurtosis.html

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

3 changes: 2 additions & 1 deletion docs/reference/cmean.html

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

3 changes: 2 additions & 1 deletion docs/reference/cmedian.html

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

3 changes: 2 additions & 1 deletion docs/reference/crange.html

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

3 changes: 2 additions & 1 deletion docs/reference/csd.html

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

3 changes: 2 additions & 1 deletion docs/reference/cskewness.html

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

3 changes: 2 additions & 1 deletion docs/reference/cvar.html

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

Loading

0 comments on commit a62510d

Please sign in to comment.