Search code examples
rdataframedplyrtibble

Summarizing rows of a data frame in R


I tried the following to convert my Data into my Desired_output without success.

I wonder what I'm missing?

library(tidyverse)

group_by(Data, ID) %>% 
  summarise(Den=Den, 
            Num=Num, 
            Num_devidedby_Den = Num/Den)
Data = read.table(h=T, text="
ID Type  Num  Den
A   1    2   3
A   2    5   6
B   1    1   7")

### Please let R evaluate + or / signs!
Desired_output = "
ID Type  Num  Den  Num_devidedby_Den
A   1    2   3       2/3
A   2    5   6       4/6
A   ALL  2+5 3+6     7/9
B   1    1   7       1/7
B   ALL  1   7       1/7"

Solution

  • If I understand correctly, in the summary by group, you need to wrap the values in sum() - ie, Num = sum(Num) then bind it back together and arrange:

    Data %>% 
      summarise(Den=sum(Den), 
                Num=sum(Num), 
                Num_devidedby_Den = Num/Den,
                Type = "ALL",
                .by = ID) %>%
      bind_rows(Data %>% mutate(Num_devidedby_Den = Num/Den, 
                                Type = as.character(Type))) %>% 
      arrange(ID, Type)
    

    Output:

      ID Den Num Num_devidedby_Den Type
    1  A   3   2         0.6666667    1
    2  A   6   5         0.8333333    2
    3  A   9   7         0.7777778  ALL
    4  B   7   1         0.1428571    1
    5  B   7   1         0.1428571  ALL