In R, I would like to create new variables in a data frame by making some computations between specific existing variables. The variable name of the new variables, and the particular existing variables to be used in the computations is (or should be ) defined by a regular expression.
I know the description is kind of confusing, so here an example with an imaginary data set where some variables (V1, V2, V3) were measured at 2 different time-points (T1, T2):
dataframe <- data.frame(matrix(rnorm(70), nrow=10))
names(dataframe) <- c("Subject", "V1_T1", "V1_T2", "V2_T1", "V2_T2", "V3_T1", "V3_T2")
dataframe$subject <- factor(dataframe$Subject)
Now, for each subject, and each "Tn" (T1, T2, T3) I would like to generate a new variable (in the same data frame), which should be the result of an operation between different variables with the same "Tn". Here some pseudo-code to try to explain my needs a bit more clearly (I hope)
for i in c(T1, T2, T3){ #For each timepoint (& Subject)...
dataframe$V4_*i* <- V1_*i* + V2_*i* / V3_*i* #Compute V4 = V1 + V2 / V3
}
This should result in several new V4_n variables (V4_T1, V4_T2, V4_T3) corresponding to the result of the V1 + V2 / V3 operation for each time-point Tn and each Subject.
In short, I would like to use regular expressions and for-loops to name and compute new variables, looping a predefined operation over variables specified by something like a regular expression. (It is not mandatory that I use for loops or regular expressions, If there are alternative methods to achieve what I want I would like to hear about them)
I have been toying a bit with the for-loop and regular expression documentation in R, but so far I have not been successful in producing the desired result. I can of course manually write down all required computations in regular R script, one by one, but that is not efficient at all (considering that the actual data-set where I need to apply this is far more complex than this one), and it is pretty annoying to have to copy-paste and edit the same piece of code several times over (also, more susceptible to typos and errors).
Any help/suggestions would be appreciated, thanks!