I want to subset a data.frame an perform various functions. As a result I need a new data.frame.
Example data:
df <- data.frame(X1 = c(rep(c(TRUE, FALSE), 6)), X2 = c(rep(c("static", "static", "dynamic"),4 )), X3 = c(rep(2,6),rep(4,6)), X4 = rnorm(12,10,1))
creates:
X1 X2 X3 X4
1 TRUE static 2 11.516013
2 FALSE static 2 10.067730
3 TRUE dynamic 2 8.890612
4 FALSE static 2 7.773297
5 TRUE static 2 9.748178
6 FALSE dynamic 2 10.850210
7 TRUE static 4 11.651970
8 FALSE static 4 7.740650
9 TRUE dynamic 4 10.282349
10 FALSE static 4 10.514651
11 TRUE static 4 10.767866
12 FALSE dynamic 4 8.863213
I want to subset the data.frame in a way that
- all rows with the same conditions (X1,X2,X3) are selected and
- one or more functions are performed on the corresponding X4 values (e.g.: mean()) and finally
the result is written in a new dataframe, displaying all possible combinations (of X1:X3):
X1 X2 X3 X4_mean X4_SD TRUE static 2 11.516013 NA ... ... ... ... ...
Thanks in advance!