Home > Blockchain >  R - add row to a data frame that calculates sum of all values in a column
R - add row to a data frame that calculates sum of all values in a column

Time:01-26

I have a data frame with first two columns characters and the rest doubles I want to add a row in the end that has the word 'Total' in the first two columns and calculates the sum of the column values in the rest for example let's use

segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg3', 'seg3')
subSegment<- c('subseg1', 'subseg2', 'subseg1', 'subseg2', 'subseg1', 'subseg2')
var.1<- c(100, 20, 30, 50, 40, 40)
var.2<- c(200, 30, 30, 70, 30, 140)
var.3<- c(50, 50, 40, 20, 30, 40)
var.4<- c(60, 50, 35, 53, 42, 20)
df<- data.frame(segment, subSegment, var.1, var.2, var.3, var.4)

this is how I did it

df%>% #now need to add a row with totals
    add_row( segment="Total",subSegment="Total", var.1 = sum(.$var.1), var.2= sum(.$var.2), var.3 = sum(.$var.3), var.4 = sum(.$var.4))

obviously the names and values are just examples but in my real problem I have over 8 variables and doing things this way is a waste of time I am looking for a more general solution that just says to add_row with first 2 columns "Total" and then ....=sum(.$....) for all columns after the third.

Maybe using something of the sort of: sapply(df[c(3:ncol(df))]

CodePudding user response:

The janitor package has this ready to go:


library(janitor)

df %>%
  adorn_totals()

CodePudding user response:

df<- rbind(df, c("Total", "Total", colSums(df[,3:ncol(df)], na.rm = TRUE)))
  •  Tags:  
  • Related