Dealing with Rows and Columns
September 8, 2008
So, a problem that has been bugging me for a while is trying to get row and column sums, means, etc. etc. For the longest time I was assigning a column of identifiers and then using by(data, identifier, sum), or tapply (data, identifier, sum), but it typically makes your life a whole lot simpler if you don’t have to add in a column of identifiers, and if you don’t have to worry about what they’re doing to your data.
So here’s the most useful command I’ve discovered so far for this:
apply(x, margin, function)
where x is your data, margin indicates whether you want the operation performed over rows, margin = 1; columns, margin = 2; or both, margin = c(1,2); and of course function indicates the function you want performed (e.g. mean).