删除某些列上的重复行并汇总数据

时间:2020-04-23 13:43:12

标签: r

我有一些数据。这是一个虚拟数据帧作为示例:

Reference = c('A', 'A', 'A', 'B', 'C', 'D', 'E', 'E')
Company = c('Google', 'Google', 'Xbox', 'Nike', 'Apple', 'Samsung', 'Paypal', 'Paypal')
Method = c('Direct', 'Indirect', 'Direct', 'Direct', 'Direct', 'Indirect', 'Direct', 'Indirect')
Payments = c(500, 750, 100, 2000, 1100, 450, 100, 900)
DirectPayment = c(500, 0, 100, 2000, 1100, 0, 100, 0)
IndirectPayment = c(0, 750, 0, 0, 0, 450, 0, 900)

df = data.frame(Reference, Company, Method, Payments, DirectPayment, IndirectPayment)

enter image description here

如果您查看参考A,则Google会直接付款并直接付款;在参考文献E中,贝宝(Paypal)有间接付款和直接付款。

我需要摆脱对参考和公司的重复。即对于Google,我只想为参考A填写一行,在DirectPayment栏中输入直接付款,在IndirectPayment中输入间接付款,即:

anged

我该怎么做?我试过了pivot_wide,但那并不是我所需要的。

谢谢

1 个答案:

答案 0 :(得分:2)

那一个呢?

library(dplyr)
df %>% 
  group_by(Reference, Company) %>% 
  summarise_if(is.numeric, sum, na.rm = TRUE)

它给出:

# A tibble: 6 x 5
# Groups:   Reference [5]
  Reference Company Payments DirectPayment IndirectPayment
  <fct>     <fct>      <dbl>         <dbl>           <dbl>
1 A         Google      1250           500             750
2 A         Xbox         100           100               0
3 B         Nike        2000          2000               0
4 C         Apple       1100          1100               0
5 D         Samsung      450             0             450
6 E         Paypal      1000           100             900