Transpose a DataFrame over the diagonal
Description
Transpose a DataFrame over the diagonal
Usage
<DataFrame>$transpose(
...,
include_header = FALSE,
header_name = "column",
column_names = NULL
)
Arguments
…
|
These dots are for future extensions and must be empty. |
include_header
|
If set, the column names will be added as first column. |
header_name
|
If include_header is set, this determines the name of the
column that will be inserted.
|
column_names
|
Optional string naming an existing column, or a function that takes an integer vector representing the position of value (non-header) columns and returns a character vector of same length. Column position is 0-indexed. |
Details
This is a very expensive operation. Perhaps you can do it differently.
Value
A polars DataFrame
Examples
library("polars")
df <- pl$DataFrame(a = c(1, 2, 3), b = c(4, 5, 6))
df$transpose(include_header = TRUE)
#> shape: (2, 4)
#> ┌────────┬──────────┬──────────┬──────────┐
#> │ column ┆ column_0 ┆ column_1 ┆ column_2 │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ f64 ┆ f64 │
#> ╞════════╪══════════╪══════════╪══════════╡
#> │ a ┆ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ b ┆ 4.0 ┆ 5.0 ┆ 6.0 │
#> └────────┴──────────┴──────────┴──────────┘
# Replace the auto-generated column names with a list
df$transpose(include_header = FALSE, column_names = c("x", "y", "z"))
#> shape: (2, 3)
#> ┌─────┬─────┬─────┐
#> │ x ┆ y ┆ z │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 │
#> ╞═════╪═════╪═════╡
#> │ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ 4.0 ┆ 5.0 ┆ 6.0 │
#> └─────┴─────┴─────┘
# Include the header as a separate column
df$transpose(
include_header = TRUE, header_name = "foo", column_names = c("x", "y", "z")
)
#> shape: (2, 4)
#> ┌─────┬─────┬─────┬─────┐
#> │ foo ┆ x ┆ y ┆ z │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ f64 ┆ f64 │
#> ╞═════╪═════╪═════╪═════╡
#> │ a ┆ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ b ┆ 4.0 ┆ 5.0 ┆ 6.0 │
#> └─────┴─────┴─────┴─────┘
# Use a function to produce the new column names
name_generator <- function(x) {
paste0("my_column_", x)
}
df$transpose(include_header = FALSE, column_names = name_generator)
#> shape: (2, 3)
#> ┌─────────────┬─────────────┬─────────────┐
#> │ my_column_0 ┆ my_column_1 ┆ my_column_2 │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 │
#> ╞═════════════╪═════════════╪═════════════╡
#> │ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ 4.0 ┆ 5.0 ┆ 6.0 │
#> └─────────────┴─────────────┴─────────────┘
# Use an existing column as the new column names
df <- pl$DataFrame(id = c("i", "j", "k"), a = c(1, 2, 3), b = c(4, 5, 6))
df$transpose(column_names = "id")
#> shape: (2, 3)
#> ┌─────┬─────┬─────┐
#> │ i ┆ j ┆ k │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 │
#> ╞═════╪═════╪═════╡
#> │ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ 4.0 ┆ 5.0 ┆ 6.0 │
#> └─────┴─────┴─────┘
#> shape: (2, 4)
#> ┌────────┬─────┬─────┬─────┐
#> │ new_id ┆ i ┆ j ┆ k │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ str ┆ f64 ┆ f64 ┆ f64 │
#> ╞════════╪═════╪═════╪═════╡
#> │ a ┆ 1.0 ┆ 2.0 ┆ 3.0 │
#> │ b ┆ 4.0 ┆ 5.0 ┆ 6.0 │
#> └────────┴─────┴─────┴─────┘