1

Create a matrix with (1) an indicator , (2) a condition and (3) a variable of interest x

z<-rep(1:10,each=10)
set.seed(974); cond=rbinom(100,3,0.5)
set.seed(974) ; x=rnorm(100,1,10)
All<-cbind(z,cond,x) ; All

Using the whole dataset I can compute the quantity of interest with colsums or apply. e.g. Mean per 10 rows.

colSums(matrix(as.numeric(All[,3]), nrow=10))/10
apply(matrix(as.numeric(All[,3]), nrow=10),2,mean)

# Filtered dataset.
AllFiltered<-All[All[,2]==1,]

Now, When I perform the filtering the nrow are not constant e.g. (1,2,3,3). Is there a way to perform the same action as above to every separate group of rows filtered data?

Example of Allfiltered

       z cond           x
 [1,]  1    1 -10.5135290

 [2,]  2    1 -10.9883098
 [3,]  2    1  12.9269151

 [4,]  3    1   5.1725988
 [5,]  3    1  -1.5633754
 [6,]  3    1  -1.3470068

 [7,]  4    1  12.6646369
 [8,]  4    1  -9.7694997
 [9,]  4    1   4.8618008

Would like to get the mean for every group of rows, where the number of rows after the filtering is not constant and equal to a fixed number (will change depending on the condition filter)

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • Based on the nrows of each block, you are doing colMeans on every 3 rows? – akrun Oct 18 '15 at 10:13
  • I don't understand the question. Why did you create `AllFiltered` for? How is it related to mean per 10 rows? How is your desired output related to all of this together? – David Arenburg Oct 18 '15 at 10:15
  • 1
    Are just looking for [Mean by group](http://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) ? – David Arenburg Oct 18 '15 at 10:33
  • Thank you for you comment @DavidArenburg. Apparently I was confused with the weird initial indexing I used before updating my comment. – Konstantinos Pat Oct 18 '15 at 10:49

1 Answers1

1

Based on the updated post, we may use the grouping variable as the first column

tapply(AllFiltered[,3], AllFiltered[,1], FUN= mean)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • I am interested only for variable x and would like to compute the mean for x for every filtered block before I had 10*10 rows. After the condition I have mean(x of 3 first rows) then mean(x of 3 next rows) then mean(x of 2 next rows). Therefore the number of rows I want to sum up is not constant. I would like to end up with 10 mean values of x (variable of interest) as before but this time filtered due to the condition==1. Thank you for your insights! – Konstantinos Pat Oct 18 '15 at 10:16
  • @KonstantinosPat Can you check whether this works for you? – akrun Oct 18 '15 at 10:27
  • Thank you a lot it did. I was unaware of the grouping variable option. gr <- AllFiltered[,1] ; tapply(AllFiltered[,3], gr, FUN= mean) – Konstantinos Pat Oct 18 '15 at 10:35
  • 1
    I think @akrun guess should be the right one. Also, it has to be stressed that the filter part can be avoided with `tapply(All[,3], list(All[,1],All[,2]), FUN= mean)`, which displays means for each condition and index. – nicola Oct 18 '15 at 10:36
  • @nicola and akrun. Both answers where targeted and solve my issue. Thank you again. – Konstantinos Pat Oct 18 '15 at 10:40