output <- sapply(seq(2,ncol(x),2), function(i) {
rowMeans(x[,c(i, i+1)], na.rm=T)
})
Then you can add the first column to the output
matrix.
output <- cbind(x[,1], output)
Alternatively, you can use within
:
within(x, {
pair.colmeans <- sapply(seq(2, ncol(x), 2), function(i) {
rowMeans(x[, c(i, i+1)], na.rm=TRUE)
})
})