Skip to content

Commit

Permalink
Gather list columns
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 16, 2016
1 parent 3dbcbf7 commit bae80df
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tidyr 0.4.1.9000

* `gather()` can now gather together list columns (#175).

* `unite()` and `separate()` now automatically drop removed variables from
grouping (#159, #177).

Expand Down
14 changes: 10 additions & 4 deletions src/melt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ SEXP concatenate(const DataFrame& x, IntegerVector ind, bool factorsAsStrings) {
}
break;
}
case RAWSXP:
DO_CONCATENATE(Rbyte);
case VECSXP: {
for (int j = 0; j < nrow; ++j) {
SET_VECTOR_ELT(output, i * nrow + j, VECTOR_ELT(tmp, j));
}
break;
}
default:
stop("Unsupported type (%s)", Rf_type2char(max_type));
}
}

Expand Down Expand Up @@ -183,8 +189,8 @@ List melt_dataframe(const DataFrame& data,

// Don't melt if the value variables are non-atomic
for (int i = 0; i < n_measure; ++i) {
if (!Rf_isVectorAtomic(data[measure_ind[i]])) {
stop("Can't melt data.frames with non-atomic 'measure' columns");
if (!Rf_isVector(data[measure_ind[i]])) {
stop("Can't gather non-vector column %i", measure_ind[i] + 1);
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-gather.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ test_that("can handle list-columns", {

expect_identical(out$y, df$y)
})

test_that("can gather list-columns", {
df <- dplyr::data_frame(x = 1:2, y = list(1, 2), z = list(3, 4))
out <- gather(df, k, v, y:z)
expect_equal(out$v, list(1, 2, 3, 4))
})

0 comments on commit bae80df

Please sign in to comment.