Skip to content

Commit

Permalink
Merge pull request #10 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
spsanderson authored Jul 13, 2024
2 parents 5f3b624 + 6a2a69b commit 87ca0f8
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 5 deletions.
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RandomWalker
Title: What the Package Does (One Line, Title Case)
Title: Generate Random Walks Compatible With The 'tidyverse'
Version: 0.0.0.9000
Authors@R: c(
person("Steven","Sanderson", email = "spsanderson@gmail.com", role = c("aut","cre","cph"),
Expand All @@ -15,7 +15,11 @@ URL: https://www.spsanderson.com/RandomWalker/, https://github.com/spsanderson/R
BugReports: https://github.com/spsanderson/RandomWalker/issues
Depends:
R (>= 4.1.0)
Imports:
dplyr,
tidyr
Suggests:
knitr,
rmarkdown
rmarkdown,
stats
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(rw30)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
None

## New Features
None
1. Fix #9 - Add Function `rw30()` to generate 30 random walks of 100 steps each.

## Minor Improvements and Fixes
None
5 changes: 5 additions & 0 deletions R/00_global_variables.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
globalVariables(
names = c(
"walk","x","value"
)
)
70 changes: 70 additions & 0 deletions R/rw30.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Generate Random Walks
#'
#' @family Auto Random Walk
#'
#' @author Steven P. Sanderson II, MPH
#'
#' This function generates 30 random walks with 100 steps each and pivots the
#' result into a long format tibble.
#'
#' @details
#' The function generates random walks using the normal distribution with a
#' specified mean (`mu`) and standard deviation (`sd`).
#' Each walk is generated independently and stored in a tibble. The resulting
#' tibble is then pivoted into a long format for easier analysis.
#'
#' @return A tibble in long format with columns `walk`, `x`, and `value`,
#' representing the random walks. Additionally, attributes `num_walks`,
#' `num_steps`, `mu`, and `sd` are attached to the tibble.
#'
#' @examples
#' # Generate random walks and print the result
#' rw30()
#'
#' @name rw30
NULL
#' @rdname rw30
#' @export
rw30 <- function() {

num_walks <- 30L
num_steps <- 100L
mu <- 0L
sd <- 1L

# Function to generate a single random walk
single_random_walk <- function(n, mu, sd) {
cumsum(c(0, stats::rnorm(n - 1, mu, sd)))
}

# Generate the walks
walks <- replicate(
num_walks,
single_random_walk(num_steps, mu, sd),
simplify = FALSE
)

# Create a tibble with the walks
walks_tibble <- dplyr::tibble(
x = 1:num_steps,
!!!stats::setNames(walks, paste0("walk_", 1:num_walks))
)

# Pivot the tibble longer
walks_long <- tidyr::pivot_longer(
walks_tibble,
cols = dplyr::starts_with("walk_"),
names_to = "walk",
values_to = "value"
) |>
dplyr::arrange(walk, x) |>
dplyr::select(walk, x, value) |>
dplyr::mutate(walk = factor(walk))

attr(walks_long, "num_walks") <- num_walks
attr(walks_long, "num_steps") <- num_steps
attr(walks_long, "mu") <- mu
attr(walks_long, "sd") <- sd

return(walks_long)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ navbar:
left:
- icon: "fa-home"
href: index.html
aria-lable: "Home"
- text: "Getting Started"
href: articles/getting-started.html
- text: "Function Reference"
Expand Down
4 changes: 2 additions & 2 deletions docs/news/index.html

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

34 changes: 34 additions & 0 deletions man/rw30.Rd

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

0 comments on commit 87ca0f8

Please sign in to comment.