calculate the sum of every last row

I have dynamic dataframes of n*n rows and column for example

      A	   B	C	D	E	
MA	  649	747	794	951	NA
TX	  762	NA	360	735	523
CA	  426	295	750	712	908
NY	  NA	344	366	508	492
OL	  925	991	336	NA	802
IN	  733	397	746	715	707
OR	  549	343	435	514	964
Total  889	836	NA	955	369

every time i have to calculate the total of last row, do we have any solution for this.

If the first column represents row names and the data frame is named DF, you can use

sum(DF["Total", ], na.rm = TRUE)

to get the sum of the row named Total. Is that what you need?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

to limit yourself to only numerics you could do

library(tidyverse)
mydf <- as_tibble(mtcars,rownames="carname")

filter(mydf,
       carname=="Merc 230") %>% select_if(is.numeric) %>% 
  rowSums(na.rm=TRUE)

in this case I'm isolating a relevant row, where the carname is Merc 230, for you its where some variable has the Total Cases label in it... , then select the summable columns, then sum them

tab1 <- cro_cpct(dat1$V8,dat1$Q5.7)
tab1 <- as.data.frame(tab1)
tab1[is.na(tab1)] <- 0
all <- sum(tab1["#Total cases", ], na.rm = TRUE)

getting error
Error in FUN(X[[i]], ...) :
only defined on a data frame with all numeric variables

thanks, for sollution