s-news
[Top] [All Lists]

Re: BY with stdev?

To: Eva Goldwater <goldwater@schoolph.umass.edu>, s-news@lists.biostat.wustl.edu
Subject: Re: BY with stdev?
From: Tony Plate <tplate@blackmesacapital.com>
Date: Tue, 21 Dec 2004 11:51:26 -0700
In-reply-to: <Pine.GSO.4.55.0412211317320.22443@shell1.oit.umass.edu>
References: <Pine.GSO.4.55.0412211317320.22443@shell1.oit.umass.edu>
While mean() works on a dataframe, the problem is that stdev() does not. To see exactly what your subfunction is getting passed, you can use browser() as follows:

> x <- data.frame(cbind(x1=rep(1:2,5), x2=runif(10)))
> by(x$x2,x$x1,function(xx) browser())
Called from: FUN(x[i,  , drop = F])
b(FUN)> xx
          x
1 0.6883236
3 0.1291990
5 0.1422239
7 0.3989520
9 0.7646130
b(FUN)> mean(x)
[1] 1.049702
b(FUN)> class(x)
[1] "data.frame"
b(FUN)> stdev(x)
Problem in as.double: Cannot coerce mode list to double: list(c(1., 2., 1.,
        2., 1., 2., 1., 2., 1., 2.), c(0.688323602546006,       0.7....
b(FUN)> stdev(x[[1]])
[1] 0.5270463
b(FUN)> stop
Problem: Asked from browser to stop current task
>

Now that we know that the subfunction here is getting a dataframe with one column, use an anonymous function to call stdev() appropriately:

> by(x$x2,x$x1,function(xx) stdev(xx[[1]]))
INDICES:1
[1] 0.2969911
-----------------------------------------------------------
INDICES:2
[1] 0.2061137
>


At Tuesday 11:27 AM 12/21/2004, Eva Goldwater wrote:
Hello,

I tried to get standard deviations for subgroups of a numeric column,
defined by another column. I'm using the by function, but it doesn't work
with stdev.  It's fine with mean or summarize, but not stdev or stderr.
Here is my test.  (This is S-Plus 6.2 on Windows.)

x <- data.frame(cbind(x1=rep(1:2,5), x2=runif(10)))
by(x$x2,x$x1,mean)
by(x$x2,x$x1,stdev)
 Problem in as.double: Cannot coerce mode list
 to double: list(c(0.361767916940153, 0.132468292489....
Use traceback() to see the call stack

by(x$x2,x$x1,stderr)
Problem in FUN(...X.sub.i...., str..: Too many
 arguments(1) in call to "FUN"; last 1 not matched
Use traceback() to see the call stack

What's the problem?  Incidentally, ?stderr takes me to help for the file()
function...

Thanks for your insights.  Eva

Eva Goldwater                           email: goldwater@schoolph.umass.edu
Biostatistics Consulting                Phone: (413) 545-2949
418 Arnold House                        Fax:   (413) 545-1645
715 North Pleasant Street
University of Massachusetts
Amherst, MA 01003-9304
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>