Yes, thank you. I hadn't yet seen the suggestions to use tapply. Also
a way to recast the call to stdev in by(), from Tony Plate:
"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]]))
"
Thanks all,
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
On Tue, 21 Dec 2004, Prof Brian Ripley wrote:
> On Tue, 21 Dec 2004, Eva Goldwater wrote:
>
> > Well, at least that explains why stderr refers to files. But not why
> > stddev doesn't like to be invoked by by().
> >
> > Someone suggested an alternate solution using split, but I'm the kind of
> > person that also wants to know why something that looks as it it ought to
> > work doesn't...
>
> by() passes a data frame, and stdev does not accept dataframes, but a
> `numeric vector or matrix'. (I thought I had seen several replies to that
> effect.) Use tapply() instead.
>
>
> >
> > 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
> >
> >
> > On Tue, 21 Dec 2004, Prof Brian Ripley wrote:
> >>> From: s-news-owner@lists.biostat.wustl.edu
> >>> [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Eva Goldwater
> >>> Sent: Tuesday, December 21, 2004 12:28 PM
> >>> To: s-news@lists.biostat.wustl.edu
> >>> Subject: [S] BY with stdev?
> >>>
> >>>
> >>> 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
|