Unnest a single list-column into expanded rows
Source:R/AllGenerics.R
, R/unnest2-methods.R
unnest2.Rd
Unnest a single list-column into expanded rows
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