gather
the input into long form and then separate the variable
into Curvegroup
and suffix
. spread
it back out to wide form. Rename and rearrange the columns.
library(dplyr)
library(tidyr)
DF %>%
gather(variable, value, -pos) %>%
separate(variable, c("CurveGroup", "suffix"), sep = 5, fill = "right") %>%
spread(suffix, value) %>%
select(CurveGroup, SpreadId = "pos", SpreadMonth1 = "_m1", SpreadMonth2 = "_m2",
Position = "V1")
giving:
CurveGroup SpreadId SpreadMonth1 SpreadMonth2 Position
1 BZ_SP 1 2 3 -300000
2 CL_SP 1 1 2 2540544
3 BZ_SP 2 0 0 0
4 CL_SP 2 3 4 -118621
Note: The input DF
in reproducible form is:
DF <- structure(list(pos = 1:2, BZ_SP = c(-300000L, 0L), BZ_SP_m1 = c(2L,
0L), BZ_SP_m2 = c(3L, 0L), CL_SP = c(2540544L, -118621L), CL_SP_m1 = c(1L,
3L), CL_SP_m2 = c(2L, 4L)), .Names = c("pos", "BZ_SP", "BZ_SP_m1",
"BZ_SP_m2", "CL_SP", "CL_SP_m1", "CL_SP_m2"),
class = "data.frame", row.names = c(NA, -2L))
Update: Simplified.