0

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!

HTS
  • 3
  • 3
  • Just `aggregate(X4 ~., df, mean)` ? Or `aggregate(X4 ~., df, function(x) c(mean = mean(x), sd = sd(x)))` – David Arenburg Mar 03 '15 at 09:11
  • works like a charm for one function . I will do further reading for multiple functions or just copy various data.frames into one. Thanks so far. – HTS Mar 03 '15 at 09:17
  • You don't need to do this. My second example included several functions. also, take a look at the answers in the linked question. More specifically, look into `data.table` and `dplyr` packages – David Arenburg Mar 03 '15 at 09:35

0 Answers0