Skip to content

Commit

Permalink
Allow spread to work with list cols
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jun 9, 2016
1 parent 164b9bf commit e0114f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
as "key<sep>value". Additionally, if sep is `NULL` missing values will be
converted to `<NA>` (#68).

* `spread()` works in the presence of list-columns (#199)

* `unnest()` works with non-syntactic names (#190).

* `unnest()` gains a `sep` argument. If non-null, this will rename the
Expand Down
5 changes: 5 additions & 0 deletions R/id.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ id_var <- function(x, drop = FALSE) {
} else if (length(x) == 0) {
id <- integer()
n <- 0L
} else if (is.list(x)) {
# Sorting lists isn't supported
levels <- unique(x)
id <- match(x, levels)
n <- max(id)
} else {
levels <- sort(unique(x), na.last = TRUE)
id <- match(x, levels)
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-spread.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ test_that("complex values are preserved (#134)", {
expect_equal(out2$b, 3:4 + 1i)
})

test_that("can spread with nested columns", {
df <- tibble::data_frame(x = c("a", "a"), y = 1:2, z = list(1:2, 3:5))
out <- spread(df, x, y)

expect_equal(out$a, 1:2)
expect_equal(out$z, df$z)
})

test_that("spread gives one column when no existing non-spread vars", {
df <- data_frame(
key = c("a", "b", "c"),
Expand Down

0 comments on commit e0114f4

Please sign in to comment.