filemonPi
filemonPi

Reputation: 105

working with two sets of variables in dplyr

Let's suppose I have a df with two sets of variables, A and B. I want to work column-wise by substituting column B1 with A1 and column B2 with A2:

library(dplyr)

df <- data.frame(
  A1 = c(1, 2, 3),
  A2 = c(4, 5, 6),
  B1 = c(7, 8, 9),
  B2 = c(10, 11, 12),
  COND = c("c", "c", "d")
)

  A1 A2 B1 B2 COND
1  1  4  7 10    c
2  2  5  8 11    c
3  3  6  9 12    d

Without any condition I can do:

df %>% 
  mutate(across(starts_with("B"))*(across(starts_with("A"))/across(starts_with("B"))))

  A1 A2 B1 B2 COND
1  1  4  1  4    c
2  2  5  2  5    c
3  3  6  3  6    d

Using Maël proposal , I can add a condition:


df %>%
  mutate(across(starts_with("B"), ~ case_when(COND == "c" ~ get(gsub("B", "A", cur_column())),
    .default = get(cur_column())
  )))

  A1 A2 B1 B2 COND
1  1  4  1  4    c
2  2  5  2  5    c
3  3  6  9 12    d

But still, this is possible because names of first and second sets follow a pattern and we can make use of gsub.

Would be interesting to be able to use across and select variables not following this particular pattern (i.e: Ax, Bx).

set1 <- c("ABC", "DEF")
set2 <- c("IOP", "JKL")

  ABC DEF IOP JKL COND
1   1   4   7  10    c
2   2   5   8  11    c
3   3   6   9  12    d

Upvotes: 0

Views: 60

Answers (0)

Related Questions