使用dplyr模拟split()group_by:返回数据帧列表 [英] Emulate split() with dplyr group_by: return a list of data frames

查看:155
本文介绍了使用dplyr模拟split()group_by:返回数据帧列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的数据集,在R中扼制 split()。我可以使用 dplyr group_by这是一个首选方式),但是我不能将所得到的 grouping_df 作为数据帧列表,这是我的连续处理步骤所需的格式(我需要强制



考虑一个样本数据集:



(c(a,a,b,b,c),c(1,2) ,3,4,5),c(2,3,4,2,2)))
listDf = split(df,df $ V1)

返回

  $ a 
V1 V2 V3
1 a 1 2
2 a 2 3


$ b V1 V2 V3
3 b 3 4
4 b 4 2

$ c
V1 V2 V3
5 c 5 2

我想用 group_by (类似 group_by(df,V1))的模拟效果,但这会返回一个, grouping_df 。我知道 do 应该可以帮助我,但我不确定使用情况(另见链接



请注意,按照用于建立此组的因子的名称对每个列表进行分配 - 这是一个所需的功能(最终,用于提取这些名称的方式的奖金kudos从dfs列表中)。

解决方案

要贴到dplyr,您还可以使用 plyr 而不是 split

  plyr)

dlply(df,V1,身份)
#$ a
#V1 V2 V3
#1 a 1 2
#2 a 2 3


$ b#V1 V2 V3
#1 b 3 4
#2 b 4 2

#$ c
#V1 V2 V3
#1 c 5 2


I have a large dataset that chokes split() in R. I am able to use dplyr group_by (which is a preferred way anyway) but I am unable to persist the resulting grouped_df as a list of data frames, a format required by my consecutive processing steps (I need to coerce to SpatialDataFrames and similar).

consider a sample dataset:

df = as.data.frame(cbind(c("a","a","b","b","c"),c(1,2,3,4,5), c(2,3,4,2,2)))
listDf = split(df,df$V1)

returns

$a
   V1 V2 V3
 1  a  1  2
 2  a  2  3

$b
   V1 V2 V3
 3  b  3  4
 4  b  4  2

$c
   V1 V2 V3
 5  c  5  2

I would like to emulate this with group_by (something like group_by(df,V1)) but this returns one, grouped_df. I know that do should be able to help me, but I am unsure about usage (also see link for a discussion.)

Note that split names each list by the name of the factor that has been used to establish this group - this is a desired function (ultimately, bonus kudos for a way to extract these names from the list of dfs).

解决方案

To 'stick' to dplyr, you can also use plyr instead of split:

library(plyr)

dlply(df, "V1", identity)
#$a
#  V1 V2 V3
#1  a  1  2
#2  a  2  3

#$b
#  V1 V2 V3
#1  b  3  4
#2  b  4  2

#$c
#  V1 V2 V3
#1  c  5  2

这篇关于使用dplyr模拟split()group_by:返回数据帧列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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