r - Final row with the sum of the previous results -
this question has answer here:
- how add row data frame totals? 9 answers
i want generate result includes final row find sum of previous results. have 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 3 0.500 2 s 2 0.333 3 t 1 0.167
dessired result:
group q freq (fctr) (int) (dbl) 1 3 0.500 2 s 2 0.333 3 t 1 0.167 total 6 1
how can generate total row?
thank much.
the closest in r this:
list(as.data.frame(result),"total" = colsums(result[,-1]))
which preferred if want keep in within r. if need table printing purposes (pdf,html) etc., using print.table example, go this:
result <- as.data.frame(result) result["total",] <- c(na,colsums(result[,-1]))
and later replace na. quite dirty way , makes quite unusable further calculations.
also asked before, kind of: how add row data frame totals?
Comments
Post a Comment