s-news
[Top] [All Lists]

Re: "by" question

To: "'Kurbat, Matt BGI SF'" <Matt.Kurbat@barclaysglobal.com>, s-news@lists.biostat.wustl.edu
Subject: Re: "by" question
From: "Austin, Matt" <maustin@amgen.com>
Date: Thu, 13 Apr 2006 19:45:06 -0700
I'm not sure why the min does not work in the by() function.  I have
provided a method that does work.

Also, please be careful about the way you create the data.  When you mixed
character and numerics in the vectors, it coerces all to characters.  When
you created your dataframe, everything comes out as character.  Check out my
example as how to create the dataframe I think you wanted.


tempdata<-as.data.frame(matrix(NA,4,4))
        names(tempdata)<-c("yearmon","h0","h1","name")
        tempdata[1,]<-c(200602,1,4,"abvol")
        tempdata[2,]<-c(200603,2,3,"abvol")
        tempdata[3,]<-c(199106,3,2,"accr")
        tempdata[4,]<-c(199107,4,1,"accr")

is.numeric(tempdata$yearmon)

tempdata <- data.frame(yearmon= c(200602, 200603, 199106, 199107),
                       h0     = 1:4,
                       h1     = 4:1,
                       name   = c(rep(c('abvol', 'accr'), each = 2)))
is.numeric(tempdata$yearmon)

by(tempdata[, 1], tempdata$name, mean)
by(tempdata[, 1], tempdata$name, function(x) min(x))

--Matt

Matt Austin
Statistician
Amgen, Inc
800 9AMGEN9 x77431
805-447-7431




-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of Kurbat, Matt
BGI SF
Sent: Thursday, April 13, 2006 7:15 PM
To: s-news@lists.biostat.wustl.edu
Subject: [S] "by" question 


Hello, 

I've recently returned to Splus after a long layoff (forced SAS treatments).

I'm trying to apply "by" for various input functions and datasets and having
trouble getting it right.

For example, I can create a dataframe as follows: 
        tempdata<-as.data.frame(matrix(NA,4,4))
        names(tempdata)<-c("yearmon","h0","h1","name")
        tempdata[1,]<-c(200602,1,4,"abvol")
        tempdata[2,]<-c(200603,2,3,"abvol")
        tempdata[3,]<-c(199106,3,2,"accr")
        tempdata[4,]<-c(199107,4,1,"accr")
With results that look like this:
        > tempdata
          yearmon h0 h1  name 
        1  200602  1  4 abvol
        2  200603  2  3 abvol
        3  199106  3  2  accr
   4  199107  4  1  accr

I want to compute various functions (min, max, mean, stdev, kurtosis, etc.)
over the groups defined in the "name" variable using the "by" function.  
On some data sets I can get "mean" to work fine but not the others.  

On the example above, I get the following results using "mean"
        by(as.data.frame(tempdata[,1]), tempdata$name, mean)
                > by(as.data.frame(tempdata[, 1]), tempdata$name, mean)
                tempdata$name:abvol
                [1] NA
                -----------------------------------------------------------
                tempdata$name:accr
                [1] NA

On the example above, I get the following results using "min"
        by(as.data.frame(tempdata[,1]), tempdata$name, mean)
                > by(as.data.frame(tempdata[, 1]), as.factor(tempdata$name),
min)
                Problem in NextMethod(.Generic): Can't find the generic
function "FUN" 
                Use traceback() to see the call stack
 
Would someone please explain to me how I can make these work?  
I'm running Splus 7.0 on Windows 2000.  

Thanks!
Matt 
 
This message and any attachments are confidential, proprietary, and may be
privileged.  If this message was misdirected, Barclays Global Investors
(BGI) does not waive any confidentiality or privilege.  If you are not the
intended recipient, please notify us immediately and destroy the message
without disclosing its contents to anyone.  Any distribution, use or copying
of this e-mail or the information it contains by other than an intended
recipient is unauthorized.  The views and opinions expressed in this e-mail
message are the author's own and may not reflect the views and opinions of
BGI, unless the author is authorized by BGI to express such views or
opinions on its behalf.  All email sent to or from this address is subject
to electronic storage and review by BGI.  Although BGI operates anti-virus
programs, it does not accept responsibility for any damage whatsoever caused
by viruses being passed.
--------------------------------------------------------------------
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>