I am trying to identify column names with matching substrings, and then calculate the differences of the values in those columns.
Sample data:
V1_ABC <- c(1,2,3,4)
V2_ABC <- c(2,3,4,5)
V1_WXYZ <- c(10,11,12,13)
V2_WXYZ <- c(11,12,13,14)
Date <- c(2001,2002,2003,2004)
So df looks like:
df <- data.frame(Date, V1_ABC, V2_ABC, V1_WXYZ, V2_WXYZ)
Date V1_ABC V2_ABC V1_WXYZ V2_WXYZ
1 2001 1 2 10 11
2 2002 2 3 11 12
3 2003 3 4 12 13
4 2004 4 5 13 14
I want to calculate V1 minus V2 for ABC and WXYZ. My original dataset is much larger, so I don't want to do this manually for each. I'd like to automate this so that R compares the column headers and finds which columns have the same ending substring (V1_ABC and V2_ABC, and V1_WXYZ and V2_WXYZ), then subtracts the V2_ from the V1_. Like this:
Date V1_ABC V2_ABC V1_WXYZ V2_WXYZ dif_ABC dif_WXYZ
1 2001 1 2 10 11 -1 -1
2 2002 2 3 11 12 -1 -1
3 2003 3 4 12 13 -1 -1
4 2004 4 5 13 14 -1 -1
Most of the functions I have found such as grep or intersect either look for a specific string you input, or return the values where the vectors are the same.
Any ideas on how to automate pairing based on names/substrings?