Say I have a dataframe D1
with 4 columns:
F1
contains factors[A, B]
F2
contains factors[P, Q]
F3
contains factors[X, Y]
F4
contains numeric values
How can I transform this into a new three columned dataframe, D2
:
F1
andF2
as beforeF3
contains the mean of all values in previous rows averaged over all levels of[X, Y, Z]
and store this in a new dataframe.
I know how to get the mean per single factor level, but I would like to do this for the cross product of two other levels.
example given the dataframe:
F1 F2 F3 F4
A P X 2
A P Y 4
A Q X 3
A Q Y 5
B P X 1.5
B P Y 2.5
B Q X 0
B Q Y 1
the following dataframe would be outputed:
F1 F2 F3
A P 3
A Q 4
B P 2
B Q 0.5
Ideally this should work irrespective of the number of levels of factors invovled