Search code examples
rnamesdplyr

How to use dplyr:mutate to mulitply pairs of columns specified by parts of the variable name


I have the following example:

df <- data.frame(
id = c(1,2,3),
  fix_01.2012 = c(2,5,7),
  fix_02.2012 = c(5,1,7),
  fix_03.2012 = c(6,1,5),
  fox_01.2012 = c(0.4, 0.5, 0.7),
  fox_02.2012 = c(0.6, 0.5, 0.8),
  fox_03.2012 = c(0.7, 0.5, 0.9)
  )

  id fix_01.2012 fix_02.2012 fix_03.2012 fox_01.2012 fox_02.2012 fox_03.2012
1  1           2           5           6         0.4         0.6         0.7
2  2           5           1           1         0.5         0.5         0.5
3  3           7           7           5         0.7         0.8         0.9

The table below is what I want to get. I want to create a new column for each date (e.g. "01.2012"):

res_date = fix_date * fox_date

As I have many dates / pairs of dates, I guess this needs to be done by looping through the names.

 id fix_01.2012 fix_02.2012 fix_03.2012 fox_01.2012 fox_02.2012 fox_03.2012 res_01.2012 res_02.2012 res_03.2012
1  1           2           5           6         0.4         0.6         0.7         0.8         3.0         4.2
2  2           5           1           1         0.5         0.5         0.5         2.5         0.5         0.5
3  3           7           7           5         0.7         0.8         0.9         4.9         5.6         4.5

Anyone can help? Thanks very much in advance!


Solution

  • Much more verbose than the other answers, but to my eye easier to read/edit/adapt, is a heavy gather-spread approach (the way I'd reason the problem if I was solving it step-by-step):

    library(tidyr)
    library(dplyr)
    
    df %>% 
      gather(-id, key=colname, value=value) %>% 
      separate(colname, c('fixfox', 'date'), sep='_') %>% 
      spread(key=fixfox, value=value) %>% 
      mutate(res=fix*fox) %>% 
      gather(-id, -date, key=colname, value=value) %>% 
      unite(new_colname, colname, date, sep='_') %>% 
      spread(key=new_colname, value=value)