0

I want to generate a result that includes also a final row find the sum of the previous results. I have the following scenario.

Input data:

Group <- c("A","S","A","T","A","S")
Id <- c(25, 34, 28, 52, 3 ,5)
dataframe <- data.frame(Group, Id)
dataframe

Current function:

result = group_by(dataframe,Group) %>%
  summarise(q = n()) %>%
  mutate (Freq = round(q / sum(q), 3))

result

Current result:

   Group     q  Freq
  (fctr) (int) (dbl)
1      A     3 0.500
2      S     2 0.333
3      T     1 0.167

Dessired result:

   Group     q  Freq
  (fctr) (int) (dbl)
1      A     3 0.500
2      S     2 0.333
3      T     1 0.167
Total        6 1

How can I generate the total row?

Thank you very much.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Igor
  • 15
  • 2
  • you could use rbind(result,colSums(result). But R isn't really meant for this.. Excel would be your better option. – Ted Mosby Feb 25 '16 at 15:50
  • Since a total row isn't an observation, this is considered poor practice, as it will make doing anything with your data (plotting, summarizing) harder. If it's for presentation purposes there are ways, but you need to specify your medium. – alistaire Feb 25 '16 at 16:04
  • `result["Total",] <- colSums(result)` given you have converted `result` to a regular `data.frame`, defined `row.names(result) = result$Group` and lastly dropped the `Group` col althogether `result$Group <- NULL` – mtoto Feb 25 '16 at 16:06
  • @alistaire The purpose of adding a final row is to export the result with xtable to sweave – Igor Feb 26 '16 at 16:10
  • Gotcha. `knitr` is easier to work with than `Sweave`, though `xtable` can work with either. `knitr`'s built-in `kable` is worth a look, though, too, depending on what you're going for. – alistaire Feb 26 '16 at 20:18

1 Answers1

1

The closest you could get to in R would be something like this:

list(as.data.frame(result),"Total" = colSums(result[,-1]))

which would be preferred if you want to keep in within R. If you need a table for printing purposes (pdf,html) etc., using print.table for example, you would go with something like this:

result <- as.data.frame(result)
result["Total",] <- c(NA,colSums(result[,-1]))

and later replace the NA. This is however quite the dirty way to do it and makes it quite unusable for further calculations.

Also this was asked before, kind of: How do I add a row to a data frame with totals?

Community
  • 1
  • 1
niid
  • 473
  • 4
  • 13
  • I have a further question. How could I do this if one of the columns is logical. For instance: `Group <- c("A","S","A","T","A","S") Id <- c("TRUE", "FALSE", "FALSE", "TRUE", "TRUE" ,"TRUE") dataframe <- data.frame(Group, Id) dataframe result = group_by(dataframe,Group,Id) %>% summarise(q = n()) %>% mutate (Freq = round(q / nrow(dataframe), 3)) result result <- as.data.frame(result) result["Total",] <- c(NA,colSums(result[,-1]))` Is not possible because `Error in colSums(result[, -1]) : 'x' must be numeric`. Is it still possible? Thank you – Igor Mar 01 '16 at 12:46
  • I answer my self. colSums(Filter(is.numeric, result)) – Igor Mar 02 '16 at 14:59