From 0881bf4c0c0cd7bd1102a50273ce2ca73fa91aa1 Mon Sep 17 00:00:00 2001 From: EllaKaye Date: Mon, 1 Jan 2024 11:05:32 +0000 Subject: [PATCH] Add `lines_to_matrix()` (closes #4) --- NAMESPACE | 1 + NEWS.md | 1 + R/read.R | 2 +- R/wrangle-helpers.R | 14 ++++++++++++++ _pkgdown.yml | 2 ++ man/lines_to_matrix.Rd | 22 ++++++++++++++++++++++ tests/testthat/test-wrangle-helpers.R | 8 ++++++++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 R/wrangle-helpers.R create mode 100644 man/lines_to_matrix.Rd create mode 100644 tests/testthat/test-wrangle-helpers.R diff --git a/NAMESPACE b/NAMESPACE index b445c8d..802d5a1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,3 +16,4 @@ export(aoc_url_input) export(extract_numbers) export(gcd) export(lcm) +export(lines_to_matrix) diff --git a/NEWS.md b/NEWS.md index 0a6d1be..4192968 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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()` diff --git a/R/read.R b/R/read.R index 072b3cb..9e71018 100644 --- a/R/read.R +++ b/R/read.R @@ -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) diff --git a/R/wrangle-helpers.R b/R/wrangle-helpers.R new file mode 100644 index 0000000..f5b5288 --- /dev/null +++ b/R/wrangle-helpers.R @@ -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 = _) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 65594bf..d78bdf4 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -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 diff --git a/man/lines_to_matrix.Rd b/man/lines_to_matrix.Rd new file mode 100644 index 0000000..041f6b6 --- /dev/null +++ b/man/lines_to_matrix.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/wrangle-helpers.R +\name{lines_to_matrix} +\alias{lines_to_matrix} +\title{Convert vectors to a matrix} +\usage{ +lines_to_matrix(lines, split = "") +} +\arguments{ +\item{lines}{a vector} + +\item{split}{character. The string to split the input on. Default is \code{""}, i.e. one character per column} +} +\value{ +A matrix where the number of rows is the length of \code{lines} and, for a split on \verb{"}, the number of columns is the same as the number of characters in each element of \code{lines}. +} +\description{ +For the default split of \verb{"}, assumes that each element of \code{lines} has the same number of characters. +} +\examples{ +lines_to_matrix(c("#.#.", "..#.", "##..")) +} diff --git a/tests/testthat/test-wrangle-helpers.R b/tests/testthat/test-wrangle-helpers.R new file mode 100644 index 0000000..50e0971 --- /dev/null +++ b/tests/testthat/test-wrangle-helpers.R @@ -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)) +})