0

I'm not going to insert all the code, but I believe you can help me just by what I'll explain below.

I have a table like this:

output<-
      date       ABC   CDE   FGH   
   1 2021-06-30  4.00  1.00  6.00  
   2 2021-07-02  1.00   NA    NA   

How do I add a sum column in the last column like this?

 output<-
       date        ABC   CDE   FGH  SUM
    1 2021-06-30  4.00  1.00  6.00  11.00
    2 2021-07-02  1.00   NA    NA   1.00

Data

structure(list(date = structure(c(18808, 18810), class = "Date"), 
    ABC = c(4, 1), CDE = c(1, NA), FGH = c(6, NA)), row.names = c(NA, 
-2L), class = "data.frame")
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • 2
    It would be easier to find a solution for you if you added a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). However, you could try something along: `output$SUM <- rowSums(output[, c("ABC", "CDE", "FGH")])` – dario Oct 18 '21 at 16:00
  • 1
    Or `output %>% mutate(SUM = rowSums(across(ABC:FGH), na.rm = TRUE))` by using `dplyr`. – Martin Gal Oct 18 '21 at 16:06
  • Thanks! Just one thing: `ABC` and `FGH` can vary. . So, can you leave it in a way that it identifies the first and last column? –  Oct 18 '21 at 16:19

0 Answers0