Matrix subtraction, but with mismatching dimensions?

Essentially, I would like to do a - b, which results in c below.

In a not-so-simplified scenario:

  • a has millions of rows and columns
  • b has only one row, but matching number of columns and column names
  • c should be same dimensions as a
a = data.frame(a = c(1, 2, 3, 4),
               b = c(2, 3, 4, 5),
               c = c(3, 4, 5, 6))

b = data.frame(a = 1,
               b = 2,
               c = 3)

c = data.frame(a = c(0, 1, 2, 3),
               b = c(0, 1, 2, 3),
               c = c(0, 1, 2, 3))

I know that this isn't really a tidyverse problem, but I would like to use tidyverse functions as much as possible, if at all possible. I tried to convert them into matrices first, as well as trying something funky with rowwise() with no luck.

Would some subtraction be apply-able (applicable?) for matching column names between the dataframes?

Any solutions out there?

library(tidyverse)

c2 <- mutate(a,
       across(everything(),
              \(x) x - pull(.env$b,cur_column())))

identical(c,c2)

here we go over the a frame and for each column we use its value minus a value we get from the b object in our environment (i.e. not in the a frame we are processing)
This results in the desired output.

1 Like

Interesting! Is there a name for this \(x) x - pull() syntax? It looks syntactically foreign to me, especially \(x) x.

thats an anonymous function.

Introduced in R 4.1.0

R now provides a shorthand notation for creating functions, e.g. \(x) x + 1 is parsed as function(x) x + 1.

rather than typing function I can type \

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.