I have 400 rows that have a bunch of columns, with the last five being: a,b,c,d,e
For each row, I want to randomly select three of the above 5 variables and do rowmeans(varx,vary,varz) to make column trio_average, and the other two making pair_average.
For example, one row might be the mean of b,d,e for column "trio_average" and the mean of a,c for "pair_average", and the next might be the mean of a,c,e and b,d.
I did this in a pretty roundabout way...I used "randomizr()" to generate a variable called "trio_set" with 400 random (conditional random to keep them all equal) trios of the 5 variables. There's 10 possible combinations of the 5 variables so I have 40 each of for example "a_c_e", "b_c_d" etc.
Then, I used a series of 10 if_else statements:
data <- transform(data, trio_average = ifelse(trio_set = "a_b_c", rowMeans(data[c("a","b","c")]),
ifelse(trio_set = "a_b_d", rowMeans(data[c("a","b","d")]), ....
I would then do this another 10 times for the pairs.
This does get the job done but in reality, my column names are much longer than e.g. "a" so my code in the end is pretty bad looking and inefficient. Is there a better way to do this?