# Using tapply, ddply, and sqldf for the same task

##### Posted on June 08, 2012

Usually there’s more than one way to do things when programming with data. An interesting application is when we want to do some calculation on a quantitative variable that is associated to a qualitative variable. For instance, you have the prices (quantitative) and brands (qualitative) of different products, and you want to get the average price of those products by brand. To make things clearer let’s generate some imaginary data of commercial products:

You should get a table like this one

How can we obtain the average price by brand? Well, it depends on what do you want to use. Here are some options:

• using a for loop (not recommendable)
• using boolean selection
• using tapply()
• using ddply() from package "plyr"
• using a sql query (with package "sqldf")

### Option 5: using an SQL query

Published in categories how-to  Tagged with ddply  factor  query  sqldf  tapply