取消嵌套包含列表的数据框 [英] Unnesting a data frame containing lists

查看:22
本文介绍了取消嵌套包含列表的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含列表的数据框,如下所示:

I have a data frame that contains lists, like below:

# Load packages
library(dplyr)

# Create data frame
df <- structure(list(ID = 1:3, 
                     A = structure(list(c(9, 8), c(7,6), c(6, 9)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), 
                     B = structure(list(c(3, 5), c(2, 6), c(1, 5)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), 
                     C = structure(list(c(6, 5), c(7, 6), c(8, 7)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), 
                     D = structure(list(c(5, 3), c(4, 1), c(6,  5)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr"))), 
                row.names = c(NA, -3L), class = c("tbl_df", "tbl", "data.frame"))

# Peek at data 
df
#> # A tibble: 3 x 5
#>      ID A         B         C         D        
#>   <int> <list>    <list>    <list>    <list>   
#> 1     1 <dbl [2]> <dbl [2]> <dbl [2]> <dbl [2]>
#> 2     2 <dbl [2]> <dbl [2]> <dbl [2]> <dbl [2]>
#> 3     3 <dbl [2]> <dbl [2]> <dbl [2]> <dbl [2]>

我想取消嵌套列表,并且可以使用 pmap_dfr 执行此操作.

I'd like to unnest the lists and can do so using pmap_dfr.

# Expand rows
df %>% purrr::pmap_dfr(function(...)data.frame(...))
#>   ID A B C D
#> 1  1 9 3 6 5
#> 2  1 8 5 5 3
#> 3  2 7 2 7 4
#> 4  2 6 6 6 1
#> 5  3 6 1 8 6
#> 6  3 9 5 7 5

reprex 包 (v0.3.0) 于 2019 年 6 月 28 日创建

Created on 2019-06-28 by the reprex package (v0.3.0)

这是想要的结果,但似乎是在重新发明轮子,因为 tidyr::unnest 旨在将列表列展平回常规列.但是,使用 tidyr::unnest 会产生以下错误:

This is the desired result, but seems to be reinventing the wheel because tidyr::unnest is designed to flatten list columns back to regular columns. Using tidyr::unnest produces the following error, however:

df %>% unnest(cols = c(A, B, C, D))
#Error: No common type for `x` <tbl_df<A:double>> and `y` <double>.
#Call `rlang::last_error()` to see a backtrace

在这种情况下,我将如何应用 unnest 来用列表列展平我的数据框?

How would I apply unnest in this case for flattening my data frame with list columns?

> packageVersion("tidyr")
[1] ‘0.8.3.9000’

推荐答案

注意:Hadley Wickham 在 github 上将此问题标记为 tidyr 版本 0.8.3.9000 中的一个错误(请参阅 此处).在问题得到解决之前,我会将以下答案作为潜在的解决方法.

Note: Hadley Wickham has flagged this issue on github as a bug in tidyr version 0.8.3.9000 (see here). I'll leave the below answer as a potential workaround until the issue is fixed.

看起来 nest 更专门用于在 0.8.3.9000 中创建数据帧的列表列.来自文档:Nesting 创建一个列表列数据框;取消嵌套会将其展平为常规列..例如,尝试:

It looks like nest is more specifically used to create list-columns of dataframes in 0.8.3.9000. From the docs: Nesting creates a list-column of data frames; unnesting flattens it back out into regular columns.. For example, try:

df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1) %>% 
    nest(data = c(y, z))

哪个返回:

# A tibble: 3 x 2
      x           data
  <dbl> <list<df[,2]>>
1     1            [2]
2     2            [2]
3     3            [2]

接着看df$data:

<list_of<
  tbl_df<
    y: integer
    z: integer
  >
>[3]>
[[1]]
# A tibble: 3 x 2
      y     z
  <int> <int>
1     1     6
2     2     5
3     3     4

[[2]]
# A tibble: 2 x 2
      y     z
  <int> <int>
1     4     3
2     5     2

[[3]]
# A tibble: 1 x 2
      y     z
  <int> <int>
1     6     1

您的数据帧的列是向量的列表列,这似乎属于 chop 的权限,它在保留宽度的同时缩短了数据帧.例如,尝试:

Your dataframe's columns are list-columns of vectors, which seem to fall under purview of chop, which shortens a dataframes while preserving their width. For example, try:

df <- tibble(x = c(1, 1, 1, 2, 2, 3), y = 1:6, z = 6:1) %>% 
    chop(c(y, z))

哪个返回:

# A tibble: 3 x 3
      x y         z        
  <dbl> <list>    <list>   
1     1 <int [3]> <int [3]>
2     2 <int [2]> <int [2]>
3     3 <int [1]> <int [1]>

再看看df$y:

[[1]]
[1] 1 2 3

[[2]]
[1] 4 5

[[3]]
[1] 6

知道这一点,您的数据的适当方法将是 chop 的对应物 unchop,因此给定您的数据框:

Knowing this, the appropriate method for your data would be chop's counterpart unchop, so given your dataframe:

# A tibble: 3 x 5
     ID           A           B           C           D
  <int> <list<dbl>> <list<dbl>> <list<dbl>> <list<dbl>>
1     1         [2]         [2]         [2]         [2]
2     2         [2]         [2]         [2]         [2]
3     3         [2]         [2]         [2]         [2]

尝试 unchop(df, c(A, B, C, D))unchop(df, A:D),应该返回:

Try unchop(df, c(A, B, C, D)) or unchop(df, A:D), which should return:

# A tibble: 6 x 5
     ID     A     B     C     D
  <int> <dbl> <dbl> <dbl> <dbl>
1     1     9     3     6     5
2     1     8     5     5     3
3     2     7     2     7     4
4     2     6     6     6     1
5     3     6     1     8     6
6     3     9     5     7     5

这篇关于取消嵌套包含列表的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆