用管道和groupe_by()操作df

时间:2017-06-13 16:34:43

标签: r dplyr

下面是我的df

df1

我无法弄清楚如何仅在每个区域和扇区内对数字列进行基本操作 我想(max(区域中的Colonne 3和X的扇区) - 例如X

我用

开始了我的代码
df2<-
df1 %>%  
groupe_by(Region,Sector) %>%

但是我不知道如何在所有其他列上进行操作

2 个答案:

答案 0 :(得分:0)

这是你想要的吗? (顺便说一下:请确保你想要的输出是什么。我们在这里帮你解决你的具体问题,不要猜你想要什么。)

对于这个“Colonne 3”,我认为它是第4列。

df2=df1 %>% group_by(Region,Sector) %>%slice(which.max(BEST_EPS_YOY_GTH))

答案 1 :(得分:0)

对不起,我没有找到如何分享我的df ... 在下面,只是我的df的第一列 我想要计算并保存在新的df中的是例如第一行:

max(BEST_EPS_YOY_GTH,如果Region = Japan&amp;&amp; Sector =医疗保健) - (-0.12543111)

第二行: max(BEST_EPS_YOY_GTH,如果Region = The Americas&amp;&amp; Sector = Consumer discretionary) - (0.02846056)

并为我所有其他列中的所有行执行此操作(我有12个像BEST_EPS_YOY_GTH

不确定它是否清楚,但非常感谢您的帮助。

&#13;
&#13;
<html><style><!--
/* Font Definitions */
@font-face
	{font-family:"MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
	{font-family:"MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
	{font-family:"\@MS Mincho";
	panose-1:2 2 6 9 4 2 5 8 3 4;}
@font-face
	{font-family:"Lucida Console";
	panose-1:2 11 6 9 4 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
pre
	{mso-style-priority:99;
	mso-style-link:"Pr\00E9format\00E9 HTML Car";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:"Courier New";}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
	{mso-style-priority:99;
	mso-style-link:"Texte de bulles Car";
	margin:0cm;
	margin-bottom:.0001pt;
	font-size:8.0pt;
	font-family:"Tahoma","sans-serif";}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
span.TextedebullesCar
	{mso-style-name:"Texte de bulles Car";
	mso-style-priority:99;
	mso-style-link:"Texte de bulles";
	font-family:"Tahoma","sans-serif";}
span.PrformatHTMLCar
	{mso-style-name:"Pr\00E9format\00E9 HTML Car";
	mso-style-priority:99;
	mso-style-link:"Pr\00E9format\00E9 HTML";
	font-family:"Courier New";}
.MsoChpDefault
	{mso-style-type:export-only;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=FR link=blue vlink=purple><div class=WordSection1><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>Ticker&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sector&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Region&nbsp;&nbsp; &nbsp;BEST_EPS_YOY_GTH<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>A US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Health Care&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Japan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.12543111<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>AAP US Equity&nbsp;&nbsp; AAP US Equity Consumer Discretionary The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.02846056<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>ABBV US Equity ABBV US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Health Care The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.03884949<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>ABT US Equity&nbsp;&nbsp; ABT US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Health Care The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.15789776<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>ACN US Equity&nbsp;&nbsp; ACN US Equity Information Technology The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.19914068<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>ADBE US Equity ADBE US Equity Information Technology The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.14954818<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>AEM CN Equity&nbsp;&nbsp; AEM CN Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Materials&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Japan&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0.12508785<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>AES US Equity&nbsp;&nbsp; AES US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Utilities The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.06861536<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>AET US Equity&nbsp;&nbsp; AET US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Health Care The Americas&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.17615952<o:p></o:p></span></p><p class=MsoNormal style='line-height:10.25pt;background:#0B0A09;word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:#FCFFE0'>AFL US Equity&nbsp;&nbsp; AFL US Equity&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Financials Asia Pacific&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -0.31764175<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p></div></body></html>
&#13;
&#13;
&#13;