-3

I have a dataframe as below:

|id|Test1|Test2|Test3| 

|01|  1  |  0  |  0  |

|01|  0  |  1  |  0  | 

|01|  0  |  0  |  1  |

|02|  0  |  0  |  0  |

|02|  0  |  1  |  0  |

|02|  0  |  0  |  1  |

and have the output dataframe look like:

 |01|  1  |  1  |  1  |

 |02|  0  |  1  |  1  |

Is group by on average for each test the best way to approach this?

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Anna Huang
  • 287
  • 2
  • 5
  • 15

2 Answers2

2

With summarize_all from dplyr:

library(dplyr)

df %>% 
  group_by(id) %>% 
  summarize_all(max)
acylam
  • 18,231
  • 5
  • 36
  • 45
0

Yes, you can group by on id to get the desired result. You can use the dplyr package for this:

library(dplyr)
df_agg <- df %>% 
group_by(id) %>% 
summarise(Test1 = sum(Test1), Test2 = sum(Test2), Test3 = sum(Test3)
SmitM
  • 1,366
  • 1
  • 8
  • 14