Skip to contents

Unnest a single list-column into expanded rows

Usage

unnest2(object, ...)

# S4 method for DFrame
unnest2(object, col)

Arguments

object

Object.

col

character(1). Name of the list-column to unnest into long format.

...

Additional arguments.

Value

Modified object, with a single unnested list-column expanded into rows.

Note

Updated 2023-08-25.

See also

Examples

## DFrame ====
suppressPackageStartupMessages({
    library(IRanges)
    library(pipette)
})
object <- as.DataFrame(list(
    "col1" = CharacterList(
        c("a", "b", "c", "d"),
        c("e", "f", "g"),
        c("h", "i")
    ),
    "col2" = IntegerList(
        seq(from = 1L, to = 2L),
        seq(from = 3L, to = 4L),
        seq(from = 5L, to = 6L)
    ),
    "col3" = c("a", "b", "c")
))
print(object)
#> DataFrame with 3 rows and 3 columns
#>              col1          col2        col3
#>   <CharacterList> <IntegerList> <character>
#> 1       a,b,c,...           1,2           a
#> 2           e,f,g           3,4           b
#> 3             h,i           5,6           c
x <- unnest2(object, col = "col1")
print(x)
#> DataFrame with 9 rows and 3 columns
#>          col1          col2        col3
#>   <character> <IntegerList> <character>
#> 1           a           1,2           a
#> 2           b           1,2           a
#> 3           c           1,2           a
#> 4           d           1,2           a
#> 5           e           3,4           b
#> 6           f           3,4           b
#> 7           g           3,4           b
#> 8           h           5,6           c
#> 9           i           5,6           c
y <- unnest2(object, col = "col2")
print(y)
#> DataFrame with 6 rows and 3 columns
#>              col1      col2        col3
#>   <CharacterList> <integer> <character>
#> 1       a,b,c,...         1           a
#> 2       a,b,c,...         2           a
#> 3           e,f,g         3           b
#> 4           e,f,g         4           b
#> 5             h,i         5           c
#> 6             h,i         6           c