diff --git a/R/expr__array.R b/R/expr__array.R index 65a0854b0..7c5323122 100644 --- a/R/expr__array.R +++ b/R/expr__array.R @@ -278,6 +278,22 @@ ExprArr_shift = function(periods = 1) { unwrap("in $arr$shift():") } + +#' Convert an Array column into a List column with the same inner data type +#' +#' @return [Expr] of [data type List][DataType_List] +#' @examples +#' df = pl$DataFrame( +#' a = list(c(1, 2), c(3, 4)), +#' schema = list(a = pl$Array(pl$Int8, 2)) +#' ) +#' +#' df$with_columns( +#' list = pl$col("a")$arr$to_list() +#' ) +ExprArr_to_list = function() .pr$Expr$arr_to_list(self) + + #' Convert array to struct #' #' @inheritParams ExprList_to_struct diff --git a/man/ExprArr_to_list.Rd b/man/ExprArr_to_list.Rd new file mode 100644 index 000000000..fde350e83 --- /dev/null +++ b/man/ExprArr_to_list.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/expr__array.R +\name{ExprArr_to_list} +\alias{ExprArr_to_list} +\title{Convert an Array column into a List column with the same inner data type} +\usage{ +ExprArr_to_list() +} +\value{ +\link{Expr} of \link[=DataType_List]{data type List} +} +\description{ +Convert an Array column into a List column with the same inner data type +} +\examples{ +df = pl$DataFrame( + a = list(c(1, 2), c(3, 4)), + schema = list(a = pl$Array(pl$Int8, 2)) +) + +df$with_columns( + list = pl$col("a")$arr$to_list() +) +} diff --git a/tests/testthat/test-expr_array.R b/tests/testthat/test-expr_array.R index d6edfe12f..7c4bab9e6 100644 --- a/tests/testthat/test-expr_array.R +++ b/tests/testthat/test-expr_array.R @@ -300,6 +300,22 @@ test_that("arr$shift", { ) }) +test_that("arr$to_list", { + df = pl$DataFrame( + strings = list(c("a", "b"), c("c", "d")), + schema = list(strings = pl$Array(pl$String, 2)) + ) + + expect_true( + df$select(pl$col("strings")$arr$to_list())$equals( + pl$DataFrame( + strings = list(c("a", "b"), c("c", "d")), + schema = list(strings = pl$List(pl$String)) + ) + ) + ) +}) + test_that("arr$to_struct", { df = pl$DataFrame( strings = list(c("a", "b"), c("c", "d")),