 <p>I would do this using <code>ddply</code> from <code>plyr</code> package. For example:</p> <pre><code>require(plyr) res &lt;- lapply(list.files(pattern='^[1-2].txt'),function(ff){ ## you read the file data &lt;- read.table(ff, header=T, quote="\"") ## remove the outlier data &lt;- data[data$RT&gt;200,] data &lt;- ddply(data,.(Condition),function(x) x[!abs(scale(x$RT)) &gt; 3,]) ## compute the mean ddply(data,.(Condition,Reqresponse,Score),summarise,RT=mean(RT)) }) [[1]] Condition Reqresponse Score RT 1 X a 0 500 2 X a 1 750 3 X b 0 500 4 X b 1 500 5 Y a 0 400 6 Y a 1 640 7 Y b 1 1000 8 Z a 0 1000 9 Z a 1 1675 10 Z b 0 400 [[2]] Condition Reqresponse Score RT 1 X a 0 500 2 X a 1 750 3 X b 0 500 4 X b 1 500 5 Y a 0 400 6 Y a 1 640 7 Y b 1 1000 8 Z a 0 1000 9 Z a 1 1675 10 Z b 0 400 </code></pre>