s-news
[Top] [All Lists]

class and inheritance

To: <s-news@lists.biostat.wustl.edu>
Subject: class and inheritance
From: "Russell Ivory" <Russell.Ivory@MerrickBank.com>
Date: Tue, 30 Jan 2007 16:21:24 -0700
Cc: "David Wiltsee" <david.wiltsee@MerrickBank.com>, "Keith McMillan" <Keith.McMillan@MerrickBank.com>
Thread-index: AcdExVviXCaICHX+Rly8SymYshI84w==
Thread-topic: [S] class and inheritance
Hi S list.

I need to define a new class for credit bureau data so that I can create
new methods for dealing with special missing codes, etc. If I assign the
new class to each vector, and then make a data frame out of these new
class vectors, all seems to work well.  However, if I assign columns of
a data frame to the new class (cb.data) after the data frame exists,
things blow up. For example, I cannot get a correlation matrix as
follows:
(I'm using S-Plus 7.0 Professional Developer)
 
#Creating both integer and numeric data vectors and a data frame
> test1 <- c(4, 6, 5, 7, 5, 4, 5, 3, 5)
> test2 <- c(21, 65, 54, 87, 65, 32, 21, 54, 87)
> test3 <- rnorm(9, 12, 3)
> testdata <- data.frame(cbind(test1, test2, test3))

 
#Define a new class "cb.data":
> setClass("cb.data", "numeric")
      # At least the way I understand it, class cb.data extends class
        # numeric, i.e. it is a prototype of and inherits all that
        # numeric has to offer.
        # But knowing some data may be integer, I instruct that as well:

> setIs("cb.data", "integer")


#getClass shows that cb.data extends both integer and numeric:
> getClass("cb.data")

No slots; prototype of class "numeric"

Extends:
Class "integer" by direct inclusion
Class "numeric" by direct inclusion
Class "vector" indirectly through class "numeric"
Class "positionsNumeric" indirectly through class "numeric"
Class "complex" indirectly through class "numeric"
Class "positions" indirectly through class "positionsNumeric"


#Now I assign the class to each column:
> for(i in 1:3)
           class(testdata[, i]) <- "cb.data"


#Correlations seem to be complex, if I read the output correctly
> cor(testdata[, 1], testdata[, 2])
[1] 0.5611317+0i
> cor(testdata[, 1], testdata[, 3])
[1] -0.08105978+0i

 
#And I get an error trying to create the correlation matrix:
> cor(testdata)
Problem in numerical.matrix(x): Non-atomic (list) variables are not
allowed 
Use traceback() to see the call stack



Any suggestions are greatly appreciated.

Russell Ivory | AVP Modeling Manager | Merrick Bank | (801) 545-6640 |
russell.ivory@merrickbank.com

10705 South Jordan Gateway
Suite 200
South Jordan, UT 84095
****************************************************************************This
 e-mail and any files transmitted with it are confidential and are intended 
solely for the use of the individual or entity to whom it is addressed. If you 
are not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, be advised that you have received this e-mail 
in error, and that any use, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender at Merrick Bank and delete it from your 
computer. Although Merrick Bank attempts to sweep e-mail and attachments for 
viruses, it does not guarantee that either are virus-free and accepts no 
liability for any damage sustained as a result of viruses.


<Prev in Thread] Current Thread [Next in Thread>
  • class and inheritance, Russell Ivory <=