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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: