扩展数据帧内的数据帧 [英] expand data frames inside data frame

查看:222
本文介绍了扩展数据帧内的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的数据框架:

I have a data frame with the following structure:

   language  subID sessionID                  rdm
      (chr) (fctr)    (fctr)                (chr)
1     Dutch  13602     13257 <data.frame [676,3]>
2     Dutch  13602    125354 <data.frame [676,3]>
3     Dutch  17790     19308 <data.frame [676,3]>
4     Dutch  37016     38221 <data.frame [676,3]>
5     Dutch  46830     47890 <data.frame [676,3]>

我想将这个数据帧转换成676 * n乘6个数据帧 - 换句话说,取rdm数据框列,并将其扩展到较大的数据框。

I would like to transform this data frame into a 676*n by 6 data frame - in other words, take the rdm column of data frames and expand it in to the larger data frame.

我将像像一样可以做某事:$ //
$ b

I would like to be able to do something like:

bind_rows(data_frame$rdm,.id=c(data_frame$sessionID,data_frame$subID,data_frame$language))

但是,显然,.id属性不能这样工作。我如何获得我以后的数据框?

But, obviously, the ".id" attribute doesn't work that way. How can I get the data frame I'm after?

推荐答案

我们可以使用 unnest $ library(tidyr)

library(tidyr)
unnest(df1, rdm)
#  Source: local data frame [6 x 4]

#  language sessionID    V1    V2
#     (chr)     (dbl) (int) (int)
#1    Dutch     13257     1     2
#2    Dutch     13257     2     3
#3    Dutch     13257     3     4
#4    Dutch    125354     4     5
#5    Dutch    125354     5     6
#6    Dutch    125354     6     7



数据



data

library(dplyr)
df1 <- data_frame(language=c('Dutch', 'Dutch'), sessionID=c(13257, 125354),
  rdm= list(data.frame(V1=1:3, V2=2:4), data.frame(V1=4:6, V2=5:7)))

这篇关于扩展数据帧内的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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