Ran an experiment with multiple trials, wide format data (i.e., each subject has a row and information pertaining to each of those subject's trials is contained in the columns).
The measurements collected for each trial are identical, such that for each participant there are columns with the same names followed by the relevant trial number e.g., x1, y1, x2, y2 where x and y are measurements collected at each trial and 1 and 2 represent trials 1 and 2 respectively.
I am creating some new variables based on the values for each trial e.g., x1 and y1 should be joined to create x1y1 (the particular functions likely don't matter, as I have been successful in writing them for the first trial). Now I am trying to apply those functions across the multiple trials (which again are identified by the number that follows the variable name) without writing a line of code for each trial 1:n that replicates the code I've written for trial 1. My question is whether I can use apply or a for loop that looks through the column names for the structure/numbering.
Suppose I want to do the following:
XY_1 = paste0(X1,Y1)
but for each trial 1-n. XY_n = paste0(Xn,Yn)
.
Perhaps something like:
for (trial in c(1:50)){XY[trial] = paste0(X[trial], Y[trial])}
I would like the new variables to be output as columns in the data file. Thank you for your help!