I have a table with 6 columns Z1 to Z6, and I want to calculate the absolute value of the difference between each of these columns.
So far, I enumerate all the differences in a mutate command:
FactArray <- FactArray %>% mutate(diff12 = abs(Z1-Z2),
diff13 = abs(Z1-Z3),
diff14 = abs(Z1-Z4),
diff15 = abs(Z1-Z5),
diff16 = abs(Z1-Z6),
diff23 = abs(Z2-Z3),
diff24 = abs(Z2-Z4),
diff25 = abs(Z2-Z5),
diff26 = abs(Z2-Z6),
diff34 = abs(Z3-Z4),
diff35 = abs(Z3-Z5),
diff36 = abs(Z3-Z6),
diff46 = abs(Z4-Z6),
diff56 = abs(Z5-Z6))
But I realise this is error prone and will have to be rewritten if I have a different number of columns.
Is there any way to do this "automatically"? I mean in a way such as it would adjust itself if I am considering any number of columns?
Best,
Damien