1

I have a data set (Orig.data) of multiple columns: 1st col = subjects, and subsequent columns are factors (Factor1, Factor2,...) with each factor having 2 levels (A & B for Factor1, 1 & 2 for Factor2, for example); and then the results in different columns (dependent variables).

Each condition consisted of two trials. So the first two rows represent trial 1&2 of Subject1, Factor1-A, Factor2-1, 3rd and 4th rows are the trials of Sub1 Factor1-A, Factor2-2, 5th and 6th rows are Sub1 of Factor1-B, Factor2-1, and so on.

I want to calculate the mean of the first two rows, next two rows, and so on. Using the code below, I get, structurally, the output that I want, but the calculation is completely off:

MySummaryFunc <- function(x) {c(mean = mean(x, na.rm = T), len = sum(!is.na(x)))}

data_avg = summaryBy(Depend.Var1 ~ Subject + Factor1 + Factor2, 
                     data = Orig.data, 
                     FUN = MySummaryFunc, keep.names = TRUE)

Can somebody help me how to fix the problem?

phiver
  • 19,366
  • 14
  • 36
  • 42
Masa
  • 11
  • 2
  • 2
    Just try with `dplyr` `Orig.Data %>% group_by(Factor1, Factor2) %>% summarise_at(vars(matches("Depend")), funs(mean, sd, sum))` specify the `na.rm = TRUE` if needed – akrun Nov 01 '18 at 17:34
  • `aggregate(Depend ~ Factor1 + Factor1, Orig.data, MySummaryFunc)`. – Rui Barradas Nov 01 '18 at 17:40
  • Possible duplicate of [Mean of variable by two factors](https://stackoverflow.com/questions/16664119/mean-of-variable-by-two-factors) – Rui Barradas Nov 01 '18 at 17:42
  • Rui Barradas Thank you for the comments. I tried this before and didn't work. – Masa Nov 01 '18 at 17:58
  • 1
    I really don't get exactly your question. A sample data would be nice. But if i get the question: Is it possible to work if you do first rowMeans over the data you want to get the means, and then use a rollapply to get the mean of each 2 rows ? – Santiago I. Hurtado Nov 01 '18 at 18:06

0 Answers0