Skip to content

Commit

Permalink
Add lines_to_matrix() (closes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
EllaKaye committed Jan 1, 2024
1 parent d35efa9 commit 0881bf4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export(aoc_url_input)
export(extract_numbers)
export(gcd)
export(lcm)
export(lines_to_matrix)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
* Add user agent to `aoc_get_input()`
* Add `extract_numbers()` to extract all numbers from a string
* Add `gcd()` and `lcm()` for finding greatest common divisor and lowest common multiple
* Add `lines_to_matrix()`
2 changes: 1 addition & 1 deletion R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ aoc_input_matrix <- function(day, year = NULL, mode = c("character", "numeric"),

input <- readr::read_lines(aoc_input_path(day, year))

input <- matrix(unlist(strsplit(input, split = split)), nrow = length(input), byrow = TRUE)
input <- strsplit(input, split) |> do.call(rbind, args = _)

if (mode == "numeric") {
input <- apply(input, 2, as.numeric)
Expand Down
14 changes: 14 additions & 0 deletions R/wrangle-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Convert vectors to a matrix
#'
#' For the default split of `"`, assumes that each element of `lines` has the same number of characters.
#'
#' @param lines a vector
#' @param split character. The string to split the input on. Default is `""`, i.e. one character per column
#'
#' @return A matrix where the number of rows is the length of `lines` and, for a split on `"`, the number of columns is the same as the number of characters in each element of `lines`.
#' @export
#'
#' @examples lines_to_matrix(c("#.#.", "..#.", "##.."))
lines_to_matrix <- function(lines, split = "") {
strsplit(lines, split) |> do.call(rbind, args = _)
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ reference:
- aoc_input_data_frame
- aoc_input_matrix
- aoc_input_vector
- title: "Helpers for wrangling data"
- lines_to_matrix
- title: "Helper functions for working with strings"
contents:
- extract_numbers
Expand Down
22 changes: 22 additions & 0 deletions man/lines_to_matrix.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-wrangle-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# write test for lines_to_matrix
test_that("lines_to_matrix works", {
expect_equal(lines_to_matrix(c("#.#.", "..#.", "##..")),
matrix(c("#", ".", "#", ".",
".", ".", "#", ".",
"#", "#", ".", "."
), byrow = TRUE, nrow = 3))
})

0 comments on commit 0881bf4

Please sign in to comment.