Thursday, August 19, 2010

apply & sweep : Column-wise & Row-wise operations in R

How do you do operations on rows and columns in R? apply and sweep are the main tools. Here is an example of "autoscaling" (mean center and divide by standard deviation) on each column:

[I verified that this is correct on oocalc]

# 1 -> across the rows
# 2 -> across the columns 
m <- matrix(data=c(1,2,3,4,4.5,6,7,8,9,10,11,13), nrow=4, ncol=3)
m
#      [,1] [,2] [,3]
# [1,]    1  4.5    9
# [2,]    2  6.0   10
# [3,]    3  7.0   11
# [4,]    4  8.0   13

colmeans <- apply(mymatrix, 2, mean)  # the column-wise means
# mc = the column-wise mean centered data
mc <- sweep(m, 2, colmeans, "-")   # subtract is the default

col_stdev <- apply(m, 2, sd)  # column-wise standard deviations

mcstd <- sweep(mc, 2, col_stdev, "/")  # divide by standard deviations

mcstd
#            [,1]       [,2]      [,3]
# [1,] -1.1618950 -1.2558275 -1.024695
# [2,] -0.3872983 -0.2511655 -0.439155
# [3,]  0.3872983  0.4186092  0.146385
# [4,]  1.1618950  1.0883839  1.317465

No comments: