[R] Problem in summaryBy
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Feb 15 19:43:15 CET 2007
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
> The continuous information provided in this forum is fabulous as are the
> different R packages available.
> 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,T) / mean(x,T) * 100
> c(
> n = count(x),
> mean = mean(x,T),
> SD = sd(x,T),
> CV = pCV(x),
> median = median(x,T),
> min = min(x,T),
> max = max(x,T)
> )
> }
>
> library(doBy)
> # This does not produce correct minimum values
> sData <- summaryBy(AUC ~ Analyte + Dose, data=df, FUN=myStats)
> # This gives correct minimum values
> sMinData <- summaryBy(AUC ~ Analyte + Dose, data=df, FUN=min)
>
> Here are the results:
>> sData
> Analyte Dose AUC.n AUC.mean AUC.SD AUC.CV AUC.median AUC.min
> AUC.max
> 1 RBV 200 4 32.353673 11.4244263 35.311064 32.353673 1
> 46.068889
> 2 RBV 400 4 78.568606 17.0237534 21.667374 78.568606 1
> 100.974408
> 3 RBV 600 4 112.726681 9.1481701 8.115355 112.726681 1
> 128.550651
> 4 TBV 200 4 4.283628 1.3773728 32.154348 4.283628 1
> 4.837139
> 5 TBV 400 4 8.036309 0.1166662 1.451739 8.036309 1
> 8.208499
> 6 TBV 600 4 12.041576 0.1535308 1.275006 12.041576 1
> 12.179628
>> sMinData
> Analyte Dose AUC.min
> 1 RBV 200 18.720478
> 2 RBV 400 61.422756
> 3 RBV 600 108.461962
> 4 TBV 200 1.779275
> 5 TBV 400 7.944623
> 6 TBV 600 11.853593
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list