Array: subtract by row Array: subtract by row r r

Array: subtract by row


Use sweep to operate on a particular margin of the array: rows are the second dimension (margin).

sweep(a,MARGIN=2,c(1,5),FUN="-")


> library (plyr)> aaply(a, 1, "-", c(1,5) ), ,  = 1X1  1  2  1 0 -2  2 1 -1, ,  = 2X1  1 2  1 4 2  2 5 3


Use scale to subtract either the mean or a specified vector from each row, and then divide it either by the standard deviation or a specified vector.

For your example: scale(a, c(1,5), FALSE)