diff --git a/NEWS.md b/NEWS.md index 9a7e131ed..091b1ae4b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,13 +13,10 @@ - for the Expr method only, the default value for `with_replacement` is now `FALSE` (it was already the case for the DataFrame method). - - - - ### New features - New method `$has_nulls()` (#1133). +- New method `$list$explode()` (#1139). ## Polars R Package 0.17.0 diff --git a/R/expr__list.R b/R/expr__list.R index 65d85f117..b37f081da 100644 --- a/R/expr__list.R +++ b/R/expr__list.R @@ -599,3 +599,14 @@ ExprList_set_symmetric_difference = function(other) { .pr$Expr$list_set_operation(self, other, "symmetric_difference") |> unwrap("in $list$set_symmetric_difference():") } + +#' Returns a column with a separate row for every list element +#' +#' @inherit ExprList_set_union return +#' +#' @examples +#' df = pl$DataFrame(a = list(c(1, 2, 3), c(4, 5, 6))) +#' df$select(pl$col("a")$list$explode()) +ExprList_explode = function() { + .pr$Expr$explode(self) +} diff --git a/man/ExprList_explode.Rd b/man/ExprList_explode.Rd new file mode 100644 index 000000000..d3d72b8be --- /dev/null +++ b/man/ExprList_explode.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/expr__list.R +\name{ExprList_explode} +\alias{ExprList_explode} +\title{Returns a column with a separate row for every list element} +\usage{ +ExprList_explode() +} +\value{ +Expr +} +\description{ +Returns a column with a separate row for every list element +} +\examples{ +df = pl$DataFrame(a = list(c(1, 2, 3), c(4, 5, 6))) +df$select(pl$col("a")$list$explode()) +} diff --git a/tests/testthat/test-expr_list.R b/tests/testthat/test-expr_list.R index eff33755f..7f84f05dd 100644 --- a/tests/testthat/test-expr_list.R +++ b/tests/testthat/test-expr_list.R @@ -614,3 +614,15 @@ test_that("$list$set_*() casts to common supertype", { list(a = list(c("1.0", "2.0", "a", "b"), character(0))) ) }) + +test_that("$list$explode() works", { + df = pl$DataFrame(a = list(c(1, 2, 3), c(4, 5, 6))) + expect_identical( + df$select(pl$col("a")$list$explode())$to_list(), + list(a = c(1, 2, 3, 4, 5, 6)) + ) + expect_error( + df$with_columns(pl$col("a")$list$explode()), + "lengths don't match" + ) +})