使用dplyr总结R中的未知数列 [英] Summarizing unknown number of column in R using dplyr

查看:134
本文介绍了使用dplyr总结R中的未知数列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下data.frame(df)

I have following data.frame (df)

ID1 ID2 Col1 Col2 Col3 Grp
A   B   1    3    6    G1
C   D   3    5    7    G1
E   F   4    5    7    G2
G   h   5    6    8    G2

我想要实现的如下:
- 由Grp组,easy
- 然后总结,以便对于每个组,我总结列并创建列具有所有ID1和ID2的字符串

What I would like to achieve is the following: - group by Grp, easy - and then summarize so that for each group I sum the columns and create the columns with strings with all ID1s and ID2s

这将是这样的:

df %>% 
   group_by(Grp) %>% 
      summarize(ID1s=toString(ID1), ID2s=toString(ID2), Col1=sum(Col1), Col2=sum(Col2), Col3=sum(Col3))

一切都很好,我知道列数Col1,Col2,Col3),但是我希望能够实现它,以便它可以用于具有已知且总是命名为相同的ID1,ID2,Grp和任何数量的具有未知名称的附加数字列的数据帧。

Everything is fine whae Iknow the number of the columns (Col1, Col2, Col3), however I would like to be able to implement it so that it would work for a data frame with known and always named the same ID1, ID2, Grp, and any number of additional numeric column with unknown names.

有没有办法在dplyr中执行。

Is there a way to do it in dplyr.

推荐答案


喜欢能够实现它,以便它可以用于已知的数据帧,并且始终命名为相同的ID1,ID2,Grp和任何数量的具有未知名称的附加数字列。

I would like to be able to implement it so that it would work for a data frame with known and always named the same ID1, ID2, Grp, and any number of additional numeric column with unknown names.

您可以先覆盖ID列,然后再分组:

You can overwrite the ID columns first and then group by them as well:

DF %>% 
  group_by(Grp) %>% mutate_each(funs(. %>% unique %>% sort %>% toString), ID1, ID2) %>% 
  group_by(ID1, ID2, add=TRUE) %>% summarise_each(funs(sum))

# Source: local data frame [2 x 6]
# Groups: Grp, ID1 [?]
# 
#     Grp   ID1   ID2  Col1  Col2  Col3
#   (chr) (chr) (chr) (int) (int) (int)
# 1    G1  A, C  B, D     4     8    13
# 2    G2  E, G  F, h     9    11    15

我想你会在折叠到一个字符串之前统一和排序,所以我添加了这些步骤。

I think you'll want to uniqify and sort before collapsing to a string, so I've added those steps.

这篇关于使用dplyr总结R中的未知数列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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