Home > Blockchain >  How to do an operation such that all columns that start with a string subtracting all columns that e
How to do an operation such that all columns that start with a string subtracting all columns that e

Time:04-20

How to do an operation such that all columns that start with a string subtracting all columns that end with a string? I need columns A.test, B.test, C.test each subtracting A.control, B.control, C.control. To do what I need to do without using dplyr is mutate(dA=A.test-A.control, dB=B.test-B.control, dC=C.test-C.control). It looks like across and end_with only allows you to operate on one suffix but not blending two together. So I'm not sure if there's a neat way of doing this using dplyr.

CodePudding user response:

df %>%
  select(sort(names(.))) %>%
  mutate(across(contains('test'),  .names = 'd{substr(.col, 1, 1)}') - 
           across(contains('control')))

You could also consider doing:

df %>%
  rownames_to_column('rn') %>%
  pivot_longer(-rn, names_to = c('name', '.value'), names_sep = '[.]') %>%
  mutate(d = test - control)%>%
  pivot_wider(rn, values_from = c(test, control, d), names_sep = '') %>%
  rename_with(~sub('(control|test)(.)', "\\2.\\1", .), matches('control|test'))

CodePudding user response:

You could construct the second column name out of the first one:

library(dplyr)

dat %>% 
  mutate(across(ends_with("test"), 
                ~.x - get(gsub("test", "control", cur_column())),
                .names = "{gsub('test', 'diff', .col)}"))

This returns

  A.test B.test A.control B.control A.diff B.diff
1      1      3         1         2      0      1
2      2      4         1         2      1      2

Borrowed r2evans data:

dat <- data.frame(A.test=1:2, B.test=3:4, A.control=c(1,1), B.control=c(2,2))
  • Related