Skip to content
Prev 277315 / 398506 Next

max & min values within dataframe

Hi Laura,

You were close.  Just use range() instead of min/max:

## your data (read in and then pasted the output of dput() to make it easy)
dat <- structure(list(Patient = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 6L, 6L), Region = structure(c(1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("X",
"Y"), class = "factor"), Score = c(19L, 20L, 22L, 25L, 12L, 12L,
25L, 26L, 6L, 6L, 21L, 22L, 23L, 24L, 21L, 22L, 23L, 24L, 25L,
6L, 22L, 23L, 24L, 23L, 24L, 23L, 24L, 25L, 26L, 27L, 24L, 32L
), Time = c(28L, 126L, 100L, 191L, 1L, 2L, 4L, 7L, 1L, 4L, 31L,
68L, 31L, 38L, 15L, 24L, 15L, 243L, 77L, 5L, 28L, 75L, 19L, 3L,
1L, 33L, 13L, 42L, 21L, 4L, 4L, 8L)), .Names = c("Patient", "Region",
"Score", "Time"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32"))

tmp <- with(dat, aggregate(Score, list(Patient), range))
tmpreg <-  with(dat, Region[!duplicated(Patient)])

results <- data.frame(tmp$Group.1, tmpreg, tmp$x)
colnames(results) <- c("Patient", "Region", "Min", "Max")

Note it is a little tricky to get the results in a data frame, because
tmp is a bit of an odd data frame---due to the way aggregate works,
the the first column of the data frame is a regular vector, but the
second column actually contains a two column matrix.  To get it into
regular form, I extracted them separately when creating 'results'.

Cheers,

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