Skip to content

Commit

Permalink
Merge pull request #545 from matanhakim/main
Browse files Browse the repository at this point in the history
Bug fix: single-column data frame input for `row_to_names()` now works
  • Loading branch information
billdenney committed May 30, 2023
2 parents 5a34f00 + 555eceb commit 8b98f09
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# janitor 2.2.0.9000 - unreleased development version

## Breaking changes

These are all minor breaking changes resulting from enhancements and are not expected to affect the vast majority of users.

* When using `row_to_names()`, when all input values in `row_number` for a column are `NA`, `row_to_names()` creates a column name of `"NA"`, a character, rather than `NA`. If code previously used relied on a column name of `NA`, it will now error. To fix this, rely on a column name of `"NA"`.

## New features

* A new function `paste_skip_na()` pastes without including NA values (#537).

* `row_to_names()` now accepts multiple rows as input, and merges them using a new `sep` argument (#536). The default is `sep = "_"`. When handling multiple `NA` values, `row_to_names()` ignores them and only merges non-NA values for column names. When all values are `NA`, `row_to_names()` creates a column name of `"NA"`, a character, rather than `NA`.

## Bug fixes

* `adorn_totals("row")` now succeeds if the new `name` of the totals row is already a factor level of the input data.frame (#529, thanks @egozoglu for reporting).
Expand Down
7 changes: 4 additions & 3 deletions R/row_to_names.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#' Elevate a row to be the column names of a data.frame.
#'
#' @param dat The input data.frame
#' @param row_number The row of \code{dat} containing the variable names or the
#' @param row_number The row(s) of \code{dat} containing the variable names or the
#' string \code{"find_header"} to use \code{find_header(dat=dat, ...)} to find
#' the row_number.
#' the row_number. Allows for multiple rows input as a numeric vector. NA's are
#' ignored, and if a column contains only NA value it will be named \code{"NA"}.
#' @param ... Sent to \code{find_header()}, if
#' \code{row_number = "find_header"}. Otherwise, ignored.
#' @param remove_row Should the row \code{row_number} be removed from the
Expand Down Expand Up @@ -53,7 +54,7 @@ row_to_names <- function(dat, row_number, ..., remove_row = TRUE, remove_rows_ab
if (is.na(sep)) {
stop("`sep` can't be of type `NA_character_`.")
}
new_names <- sapply(dat[row_number, ], paste_skip_na, collapse = sep) %>%
new_names <- sapply(dat[row_number, , drop = FALSE], paste_skip_na, collapse = sep) %>%
stringr::str_replace_na()

if (any(duplicated(new_names))) {
Expand Down
5 changes: 3 additions & 2 deletions man/row_to_names.Rd

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

17 changes: 16 additions & 1 deletion tests/testthat/test-row-to-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ test_that("multiple rows input works", {

df_multiple_na <- example_data_row_to_names[[1]]
df_multiple_na[6:7, ] <- NA
df_multiple_na[8:10, ] <- ""

expect_equal(
suppressWarnings(
Expand Down Expand Up @@ -284,10 +285,24 @@ test_that("multiple rows input works", {

expect_equal(
suppressWarnings(
row_to_names(df_multiple_na, row_number=c(1,6,7), remove_rows_above = FALSE) %>%
row_to_names(df_multiple_na, row_number=c(1,6,7), remove_rows_above=FALSE) %>%
names()
),
c("NA", "NA")
)

expect_equal(
row_to_names(example_data_row_to_names[[1]][,1,drop = FALSE], row_number=1:5) %>%
names(),
"Title_1_2_3"
)

expect_equal(
suppressWarnings(
row_to_names(df_multiple_na, row_number=c(8:10), remove_rows_above=FALSE) %>%
names()
),
c("__", "__")
)

})

0 comments on commit 8b98f09

Please sign in to comment.