Skip to content
Prev 277349 / 398506 Next

max & min values within dataframe

Groupwise data summarization is a very common task, and it is worth
learning the various ways to do it in R. Josh showed you one way to
use aggregate() from the base package and Michael showed you one way
of using the plyr package to do the same; another way would be

ddply(df, .(Patient, Region), summarise, max = max(Score), min = min(Score))

to save on writing an explicit function. Similarly, if you have a
version of R >= 2.11.0, the aggregate() function now has a nice
formula interface, so Josh's code could also be written as

aggregate(Score ~ Patient + Region, data = df, FUN = range)

with a subsequent renaming of the variables as shown.

Other packages that could perform this task with ease include the doBy
package, the data.table package, the remix package, the Hmisc package
and, if you are comfortable with SQL, the sqldf package. For relative
novices, the doBy package is a very nice place to start because it
comes with a well written vignette and the function names correspond
well with the tasks they perform (e.g., summaryBy(), transformBy()).
The plyr and data.table packages are more general and more powerful in
terms of the types of tasks to which each is suited. Unlike
aggregate() and doBy:::summaryBy(), these packages can process
multivariable functions. As noted above, if you have an SQL
background, sqldf operates on R data objects as though they were SQL
tables, which is advantageous in complex data extraction tasks.
Package remix is useful if you want to organize results into a tabular
form that is reminiscent of SAS.

HTH,
Dennis
On Mon, Nov 14, 2011 at 8:10 AM, B Laura <gm.spam2011 at gmail.com> wrote: