Skip to content
Prev 170812 / 398502 Next

using sapply to apply function to some columns of a dataframe

On 17/02/2009 4:42 PM, mwestphal at worldbank.org wrote:
I think the easiest way to do this is to convert the dataframe into an 
array with 3 indices, and sum over one of them.  For example:

rows <- 20
cols <- 120

df <- matrix(1:(rows*cols), rows, cols)
# in your case, df <- as.matrix( df )

arr <- array( df, c(rows, 10, cols/10))
sums <- apply( arr, c(1,3), sum)

Duncan Murdoch