1

Given a situation like:

data.frame(x1 = 1:11, y1 = 3:13, x2 = 21:31, y2 = 4:14) |>
 mutate(z1 = x1 + y1,
        z2 = x2 + y2)

Resulting in:

   x1 y1 x2 y2 z1 z2
1   1  3 21  4  4 25
2   2  4 22  5  6 27
3   3  5 23  6  8 29
4   4  6 24  7 10 31
5   5  7 25  8 12 33
6   6  8 26  9 14 35
7   7  9 27 10 16 37
8   8 10 28 11 18 39
9   9 11 29 12 20 41
10 10 12 30 13 22 43
11 11 13 31 14 24 45

Is there a way to calculate multiple variables (z1, z2, ...) based on the naming scheme (different prefixes [x, y, ...] and numeric suffixes [1, 2, ...])? I guess one possibility would be to transform the data frame into long format and back. But is there an other one?

user1
  • 404
  • 1
  • 5
  • 18

1 Answers1

0

This seems like job for eval + parse:

expressions <- c("x1 + y1", "x2 + y2", "x1 + 2*y2")

res <- lapply(expressions, function(x) with(df, eval(parse(text = x))))
res <- setNames(res, paste0("z", seq_along(res)))

cbind(df, do.call(cbind, res))

Using data.table:

setDT(df)[
  , 
  (paste0("z", seq_along(expressions))) := lapply(
    expressions, function(x) eval(parse(text = x))
  )
]
det
  • 5,013
  • 1
  • 8
  • 16
  • 1
    With this solution I have to write down every expression explicitly, haven't I? (Actually this is what I want to avoid, but) what is the advantage in your solution instead of using mutate as I did? – user1 Dec 06 '22 at 14:11
  • I misunderstood the question, I thought that expression can be 'any' transformation of columns and not the sum of pairwise ones. – det Dec 06 '22 at 14:21
  • I'm unsure what you have in mind, if you say 'any' transformation, but couldn't I just write 'any' transformation inside mutate and am done? – user1 Dec 06 '22 at 14:24
  • You could, but I can't know from where you get this problem. Its totally plausible that you get your two vectors of variables (prefix) and indices (suffix) from some other part of algorithm and you want to use them to create new variables. – det Dec 06 '22 at 14:30