[R] Problem in summaryBy

Rene Braeckman RMan54 at cox.net
Thu Feb 15 19:54:34 CET 2007


Thanks for the quick response. Problem solved. Below is the corrected
script.
Rene

# Simulated simplified data
Subj  <- rep(1:4, each=6)
Analyte <- rep(c(rep("RBV",3),rep("TBV",3)),4)
Dose <- rep(c(200,400,600),8)
AUC <- rnorm(24, c(40,80,120,4,8,12), c(8,16,24,0.8,0.16,0.24))

# The real dataset may have NAs in it
df <- data.frame(Subj, Analyte, Dose, AUC)

myStats <- function(x) {
    count <- function(x) length(na.omit(x))
    pCV <- function(x) sd(x,na.rm=TRUE) / mean(x,na.rm=TRUE) * 100
    c(
      n = count(x),
      mean = mean(x,na.rm=TRUE),
      SD = sd(x,na.rm=TRUE),
      CV = pCV(x),
      median = median(x,na.rm=TRUE),
      min = min(x,na.rm=TRUE),
      max = max(x,na.rm=TRUE)
      )
}

library(doBy)

sData <- summaryBy(AUC ~ Analyte + Dose, data=df, FUN=myStats)


-----Original Message-----
From: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] 
Sent: Thursday, February 15, 2007 10:43 AM
To: Rene Braeckman
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Problem in summaryBy

On 2/15/2007 1:34 PM, Rene Braeckman wrote:
> The R script below gives values of 1 for all minimum values when I use 
> a custom function in summaryBy. I get the correct values when I use 
> FUN=min directly. Any help is much appreciated.

The man page for min lists the header as

min(..., na.rm=FALSE)

so the T you're using is being taken as data, and being coerced to 1.

Some other advice:  name your parameters in a call.  min(x, na.rm=TRUE)
would work.

Use TRUE, not T.  In R TRUE is a constant, and T is a variable name. 
You might have T <- 0, in which case

if (T) cat("TRUE!!\n") else cat("FALSE!!\n")

would print FALSE!!.

Duncan Murdoch

...



More information about the R-help mailing list