Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In R and dplyr, replace multiple calls of "mutate" with a single call using "mutate" and "across"

We have the following dataframe in R

# Create example dataframe
df <- data.frame(gp = c(0, 1, 0, 1), 
                 col1A = c(1, 2, 3, 4), 
                 col1B = c(5, 6, 7, 8), 
                 col2A = c(11, 12, 13, 14), 
                 col2B = c(15, 16, 17, 18),
                 col3A = c(11, 12, 13, 14), 
                 col3B = c(15, 16, 17, 18))

We are looking to apply the following logic:

df %>%
  dplyr::mutate(col1A = ifelse(gp == 0, col1B, col1A)) %>%
  dplyr::mutate(col2A = ifelse(gp == 0, col2B, col2A)) %>%
  dplyr::mutate(col3A = ifelse(gp == 0, col3B, col3A))

However we are looking to replace the 3 mutate calls with 1 call that combines mutate and across (or some other approach). Assume we have these variables in strings, so aVars = c('col1A', 'col2A', 'col3A') and bVars = c('col1B', 'col2B', 'col3B').

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is this type of consolidation possible to do? We’ve used mutate and across together before, but it seems more difficult to do so when using two sets of variables like we are doing with the A and the B variables here…

>Solution :

We can use cur_column() and modify it (for the trailing "B") to reference both versions of each variable.

df %>%
  mutate(
    across(ends_with("A"),
           ~ if_else(gp == 0, cur_data()[[sub("A$", "B", cur_column())]], .))
  )
#   gp col1A col1B col2A col2B col3A col3B
# 1  0     5     5    15    15    15    15
# 2  1     2     6    12    16    12    16
# 3  0     7     7    17    17    17    17
# 4  1     4     8    14    18    14    18
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading