如何在组内应用条件

时间:2018-08-20 08:28:56

标签: r dplyr grouping

我有以下数据:

  Group Hormone Damage    Score Greater   
 1 B     control damaged       4       1        
 2 B     control undamaged     4       1        
 3 B     JA      damaged       2       0       
 4 B     JA      undamaged     3       0  
 5 D     control damaged       2       0        
 6 D     control undamaged     1       0        
 7 D     JA      damaged       4       1        
 8 D     JA      undamaged     3       1        

我手动创建了“ Greater”列。本质上,它询问“在一个组内,哪个损坏的得分值更大?”分配的值越大,1越小。0。

如何创建一些代码为我做这些?我尝试使用以下dplyr代码,但未成功:

df%>% group_by(Group) %>% 
  mutate(Greater1 = if_else(Score>Score, 0,1))

如果已经提出此问题,请告诉我,因为我找不到类似的问题。

3 个答案:

答案 0 :(得分:6)

如果您没有关系的问题,您可以简单地

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
	<title>Calculator</title>
	<link rel="stylesheet" type="text/css" href="style.css">
	<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
	<link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">
  </head>
  <body>
    <div class="container">
      <input type="text" name="screen" disabled>
      <table class="calc-buttons">
        <tr class="row-1">
          <td>C</td>
          <td>CE</td>
          <td><i class="fas fa-long-arrow-alt-left"></i></td>
          <td>/</td>
        </tr>
        <tr class="row-2">
          <td>9</td>
          <td>8</td>
          <td>7</td>
          <td>*</td>
        </tr>
        <tr class="row-3">
          <td>6</td>
          <td>5</td>
          <td>4</td>
          <td>-</td>
        </tr>
        <tr class="row-4">
          <td>3</td>
          <td>2</td>
          <td>1</td>
          <td>+</td>
        </tr>
        <tr class="row-5">
          <td id="td-0">0</td>
        </tr>
      </table>
	</div>
  </body>
</html>

给出

library(tidyverse)

df %>% 
 group_by(Group, Damage) %>% 
 mutate(new = as.integer(Score == max(Score)))

答案 1 :(得分:3)

使用ave

的基本R选项
df$Greater <- with(df, as.numeric(Score == ave(Score, Group, Damage, FUN = max)))

df
#  Group Hormone    Damage Score Greater
#1     B control   damaged     4       1
#2     B control undamaged     4       1
#3     B      JA   damaged     2       0
#4     B      JA undamaged     3       0
#5     D control   damaged     2       0
#6     D control undamaged     1       0
#7     D      JA   damaged     4       1
#8     D      JA undamaged     3       1

,也可以写为

df$Greater <- with(df, ave(Score, Group, Damage, FUN = function(x) x == max(x)))

或者继续您尝试的if_else方法

library(dplyr)
df%>% 
  group_by(Group, Damage) %>% 
  mutate(Greater1 = if_else(Score==max(Score), 1,0))

答案 2 :(得分:0)

使用data.table

library(data.table)
setDT(df)[, new := as.integer(Score == max(Score)), .(Group, Damage)]