使用不同数量的元素转换嵌套数据框 [英] Transforming a nested data frame with varying number of elements

查看:71
本文介绍了使用不同数量的元素转换嵌套数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,其中有一列嵌套的数据框架具有1或2列和n行。在下面的示例中,它看起来像 df

I have a data frame with a column of nested data frames with 1 or 2 columns and n rows. It looks like df in the sample below:

'data.frame':   3 obs. of  2 variables:
 $ vector:List of 3
  ..$ : chr "p1"
  ..$ : chr "p2"
  ..$ : chr "p3"
 $ lists :List of 3
  ..$ :'data.frame':    2 obs. of  2 variables:
  .. ..$ n1: Factor w/ 2 levels "a","b": 1 2
  .. ..$ n2: Factor w/ 2 levels "1","2": 1 2
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ n1: Factor w/ 1 level "d": 1
  ..$ :'data.frame':    1 obs. of  2 variables:
  .. ..$ n1: Factor w/ 1 level "e": 1
  .. ..$ n2: Factor w/ 1 level "3": 1

df

v <- c("p1", "p2", "p3")
l <- list(data.frame(n1 = c("a", "b"), n2 = c("1", "2")), data.frame(n1 = "d"), data.frame(n1 = "e", n2 = "3"))
df <- as.data.frame(cbind(v, l))

I想要将其转换为如下所示的数据框:

I'd like to transform it to a data frame that looks like that:

[v] [n1] [n2]

p1  a  1

p1  b  2

p2  d  NA

p3  e  3




  • 如果数据框为n1和n2在单独的列中

  • 在第i行有n行的情况下,第i行的向量元素应重复n次

  • 如果n1或n2中没有内容,则应该有NA

  • 我尝试使用tidyr :: unnest,但遇到以下错误

    I've tried using tidyr::unnest but got the following error

     unnest(df)
    Error: All nested columns must have the same number of elements.
    

    有人能更好地将数据帧转换为所需格式吗?

    Does anyone has a better idea how to transform the dataframe in the desired format?

    推荐答案

    这将避免按行操作,如果您有很多行,这将很重要。

    This will avoid by-row operations, which will be important if you have a lot of rows.

    library(data.table)
    
    rbindlist(df$l, fill = T, id = 'row')[, v := df$v[row]][]
    #   row n1 n2  v
    #1:   1  a  1 p1
    #2:   1  b  2 p1
    #3:   2  d NA p2
    #4:   3  e  3 p3
    

    这篇关于使用不同数量的元素转换嵌套数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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