-1

I have a data table, see eg below:

A B C D 
1 a 2 4
2 b 3 5
3 c 4 6

with A,B,C,D as columns, I want to add a new column with sums across rows for column A,C and D. thus, new column should be

A B C D  SUM
1 a 2 4  7
2 b 3 5  9
3 c 4 6  13

Can someone please suggest.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
jalaj pathak
  • 67
  • 1
  • 8

1 Answers1

0

You can use the following code

library(tidyverse)

df <- read.table(header = TRUE, text ="A B C D

    1 a 2 4

    2 b 3 5

    3 c 4 6")

df %>% mutate(SUM = rowSums(select(df, c(A, C, D))))
UseR10085
  • 7,120
  • 3
  • 24
  • 54