1

I have a data frame where I want to create new calculated columns based on other:

dat <- data.frame(date = c("2017-02-23", "2017-02-22", "2017-02-15", 
                  "2017-02-05","2018-05-23", "2018-02-22"),
                  var1 = c(1,3,6,7,8,5), var2 = c(1,3,18,15,25,5), 
                  var3= c(8, 4,6, 5, 5,4), var4 = c(8, 4,6, 5, 5,3))

Now I want to create a data frame with this results.

dat$var5 = dat$var1 + dat$var2
dat$var6 = dat$var3 + dat$var4

But in a fast way

Fahim Ahmed
  • 141
  • 11
s157
  • 453
  • 4
  • 10
  • 1
    `transform(dat, var5 = rowSums(dat[2:3]), var6 = rowSums(dat[4:5])) ` – Ronak Shah May 30 '19 at 08:05
  • Use `.subset2` it is very efficient, e.g. `dat[['var5']] <- .subset2(dat, 2L) + .subset2(dat, 3L)` and similar for `var6`. – niko May 30 '19 at 08:24
  • @Sotos Are you sure the given link really is a duplicate? OP does not *just* ask for summation of columns but also for *efficiency*. – niko May 30 '19 at 08:28
  • 1
    @niko I was wondering this before duping it, but I think the answers given in that post are efficient. However, OP says *in a fast way*. That does not necessarily mean efficient, but could also mean without having to specify columns (look at my answer below) – Sotos May 30 '19 at 08:35
  • @Sotos That's true, possibly I misinterpreted his *fast way*. – niko May 30 '19 at 08:38

1 Answers1

1

We can use split.default to split the data frame every other column, and use Reduce to add them, i.e.

Reduce(`+`, split.default(dat[-1], c(TRUE, FALSE)))
#  var2 var4
#1    2   16
#2    6    8
#3   24   12
#4   22   10
#5   33   10
#6   10    7
Sotos
  • 51,121
  • 6
  • 32
  • 66