Data structure is:
Company Marital a single a married b widow c married b single
I'm using table(df$Company,df$Marital)
, but I want to have a column that shows the row total, such as the following:
a b c Total married 50 20 5 75 single 10 10 10 30 widow 5 50 0 55
Is there a different table function that provides a row sum append option?
Navigate to the Home tab -> Editing group and click on the AutoSum button. You will see Excel automatically add the =SUM function and pick the range with your numbers. Just press Enter on your keyboard to see the column totaled in Excel.
If you need to sum a column or row of numbers, let Excel do the math for you. Select a cell next to the numbers you want to sum, click AutoSum on the Home tab, press Enter, and you're done. When you click AutoSum, Excel automatically enters a formula (that uses the SUM function) to sum the numbers.
You could use cbind
and rowSums
afterwards:
tab <- table(df$Company,df$Marital) tab <- cbind(tab, Total = rowSums(tab))
You can also use the built-in addmargins
function:
tab <- addmargins(table(df$Company,df$Marital), 2)
(The 2
means to add a sum column, but not a sum row- you can omit it and you'll get both).
You can use addmargins
x <- table(df$Company,df$Marital) addmargins(x) # option 1 ftable(addmargins(x)) # option 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With