I have a large dataframe where certain column names partially match as shown below
>data
K55.NSC. H55.NSC. K55.TLC. H55.TLC.
1 27520.09 306.6525 284686.6 8.623333e+00 ... ...
2 57455.33 415244.7340 284693.4 1.319481e+04 ... ...
3 85977.20 814413.8720 284700.1 2.560542e+04 ... ...
4 149511.56 1629331.9228 284713.4 5.103493e+04 ... ...
5 285171.80 3213409.0205 284739.7 1.042913e+05 ... ...
6 510536.16 6233470.3062 284790.7 1.957055e+05 ... ...
I want to subtract x.NSC from x.TLC and create a new column as x.LLC with the outcomes where x refers the partially matched column name. One way to do is to make separate dataframes using 'grep':
a <- data[,grep('K55', colnames(data))]
data$k55.LLC <- a[1]-a[2]
... ...
But this is time-consuming and I find it difficult to configure a loop for this case. Is there any way to easily deal with the problem within the dataframe without creating a list? I found a similar problem here, though I'm not sure if any of the provided solutions are applicable in my case!