When I create a list within a tibble, using summarise( zzz=list(yyy)), then nest() that tibble, then later unnest() it, the structure of that list has changed. Is there someway to preserve the original structure?
Example:
TestData =
tibble::tribble(
~letter, ~value,
"a", 1,
"a", 3,
"b", 10,
"b", 30
)
stats =
TestData %>%
group_by( letter ) %>%
summarise( .groups = "keep",
mean = mean( value ),
rawGroupData = list( value )
)
stats2 = nest(stats, .key="statsdata")
stats3 = unnest_wider(stats2, col = statsdata)
str(stats)
str(stats3)
> str(stats)
gropd_df [2 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
$ letter : chr [1:2] "a" "b"
$ mean : num [1:2] 2 20
$ rawGroupData:List of 2
..$ : num [1:2] 1 3
..$ : num [1:2] 10 30
- attr(*, "groups")= tibble [2 × 2] (S3: tbl_df/tbl/data.frame)
..$ letter: chr [1:2] "a" "b"
..$ .rows : list<int> [1:2]
.. ..$ : int 1
.. ..$ : int 2
.. ..@ ptype: int(0)
..- attr(*, ".drop")= logi TRUE
> str(stats3)
gropd_df [2 × 3] (S3: grouped_df/tbl_df/tbl/data.frame)
$ letter : chr [1:2] "a" "b"
$ mean : num [1:2] 2 20
$ rawGroupData: list<list> [1:2]
..$ :List of 1
.. ..$ : num [1:2] 1 3
..$ :List of 1
.. ..$ : num [1:2] 10 30
..@ ptype: list()
- attr(*, "groups")= tibble [2 × 2] (S3: tbl_df/tbl/data.frame)
..$ letter: chr [1:2] "a" "b"
..$ .rows : list<int> [1:2]
.. ..$ : int 1
.. ..$ : int 2
.. ..@ ptype: int(0)
..- attr(*, ".drop")= logi TRUE