Skip to content

Commit

Permalink
Allow list_transpose() to work on data frames (#1141)
Browse files Browse the repository at this point in the history
Fixes #1109
---------

Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
KimLopezGuell and hadley authored Aug 20, 2024
1 parent 495581f commit 6520dc0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# purrr (development version)

* `list_transpose()` now works with data.frames (@KimLopezGuell, #1109).
* Added `imap_vec()` (#1084)
* `list_transpose()` inspects all elements to determine the correct
template if it's not provided by the user (#1128, @krlmlr).
Expand Down
3 changes: 2 additions & 1 deletion R/list-transpose.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ list_transpose <- function(x,
simplify = NA,
ptype = NULL,
default = NULL) {
vec_check_list(x)

check_list(x)
check_dots_empty()

if (length(x) == 0) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/list-transpose.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
list_transpose(10)
Condition
Error in `list_transpose()`:
! `x` must be a list, not the number 10.
! `x` must be a list, not a number.
Code
list_transpose(list(1), template = mean)
Condition
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-list-transpose.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ test_that("can transpose homogenous list", {
expect_equal(out, list(a = c(x = 1, y = 3), b = c(x = 2, y = 4)))
})

test_that("can transpose data frames", {
df <- data.frame(x = 1:2, y = 4:5)
out <- list_transpose(df)
expect_equal(out, list(c(x = 1, y = 4), c(x = 2, y = 5)))
})

test_that("transposing empty list returns empty list", {
expect_equal(list_transpose(list()), list())
})
Expand Down

0 comments on commit 6520dc0

Please sign in to comment.