[19166] in s-news-athena
[S] Summary: Subtracting a "vector" from a matrix in S-PLUS 5.1
daemon@ATHENA.MIT.EDU (Mette Langaas)
Thu Sep 9 07:03:57 1999
Message-Id: <37D79222.F51D2ACA@nr.no>
Date: Thu, 09 Sep 1999 12:55:30 +0200
From: Mette Langaas <Mette.Langaas@nr.no>
Reply-To: Mette.Langaas@nr.no
Mime-Version: 1.0
To: "s-news@wubios.wustl.edu" <s-news@wubios.wustl.edu>
Content-Type: multipart/mixed; boundary="------------BB1E09CEFE0F1D15F7524DCF"
This is a multi-part message in MIME format.
--------------BB1E09CEFE0F1D15F7524DCF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
On Tue Sept 7 I posted a question about the behaviour of S-Plus 5.1 UNIX
when subtracting a vector with class "named" from a matrix -- which
produced a vector and not a matrix as was the case in S-Plus 3.4.
I have attached the reply from Chuck Taylor at MathSoft Seattle. He
also writes that
> we have fixed this bug internally and it will not appear in
> versions of Splus 5.x after Splus 5.1.
Mette Langaas
Norwegian Computing Center
Mette.Langaas@nr.no
http://www.nr.no
--------------BB1E09CEFE0F1D15F7524DCF
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Return-Path: <chuck@statsci.com>
Received: from postal.statsci.com (IDENT:root@postal.statsci.com [206.63.206.55])
by virgo.nr.no (8.9.3/8.9.3/NR1.0) with SMTP id TAA16864
for <Mette.Langaas@nr.no>; Tue, 7 Sep 1999 19:48:21 +0200 (MET DST)
Received: from teak.statsci.com (teak [206.63.206.250])
by postal.statsci.com (8.9.2/8.9.2/Hub) with ESMTP id KAA14043
for <Mette.Langaas@nr.no>; Tue, 7 Sep 1999 10:48:38 -0700 (PDT)
Received: from localhost (chuck@localhost)
by teak.statsci.com (8.9.2/8.9.2/Client) with ESMTP id KAA16808
for <Mette.Langaas@nr.no>; Tue, 7 Sep 1999 10:48:38 -0700 (PDT)
X-Authentication-Warning: teak.statsci.com: chuck owned process doing -bs
Date: Tue, 7 Sep 1999 10:48:38 -0700 (PDT)
From: Chuck Taylor <chuck@statsci.com>
To: Mette Langaas <Mette.Langaas@nr.no>
Subject: Re: [S] Subtracting a "vector" from a matrix in S-PLUS 5.1
In-Reply-To: <37D39E39.90463F7B@nr.no>
Message-ID: <Pine.OSF.4.10.9909071026180.14989-100000@teak.statsci.com>
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
On Mon, 6 Sep 1999, Mette Langaas wrote:
> The task is to subtract a vector (with or without names) from a matrix.
> We would expect the answer to be a matrix. It seems that when the
> subtracting vector has names=NULL the answer is a matrix, and when the
> subtracting vector has names other than NULL the answer is a vector.
>
> We have seen that a vector with names attributes is not a vector in
> S-Plus, that is is.vector=F. This might be the reason for the
> behaviour.
Yes, it is. S Version 4 is much more strict about classes.
> But, in S-PLUS 3.4 UNIX the answer is always a matrix -- and that is
> what we want it to be -- and all our written code is designed for!
See below for a workaround.
> The following example is run under S-PLUS 5.1 UNIX. The matrix is called
> a and the vectors am and am.wn (with names).
>
> > a_matrix(c(1,2,3,4,5,6),ncol=2)
...
> > am_apply(a,2,mean)
...
> > am.wn_am
> > names(am.wn)_c("a1","a2")
...
> > (t(a)-am.wn)
> [1] -1 -1 0 0 1 1
...
> A solution to the above is to do as.vector on am.wn.
Notice:
> class(t(a))
[1] "matrix"
> class(am.wn)
[1] "named"
> class(am)
[1] "numeric"
> selectMethod("-", c("matrix", "named"))
function(e1, e2)
{
ans = e1@.Data - e2@.Data
names(ans) = names(e1)
ans
}
> selectMethod("-", c("matrix", "numeric"))
function(e1, e2)
if(missing(e2)) .Internal( - e1, "do_op", T, 6) else .Internal(e1 - e2,
"do_op", T, 6)
The method for c("matrix", "named") doesn't do the job, so the trick is to
convince it to use the method for c("matrix", "numeric"), which does appear
to do the job:
> setMethod("-", c("matrix", "named"),
selectMethod("-", c("matrix", "numeric")))
Now
> t(a) - am.wn
[,1] [,2] [,3]
[1,] -1 0 1
[2,] -1 0 1
The above fix creates a local method in the current database. For it to work
with another database, you would have to re-run the setMethod() function.
Another fix would be to rewrite the "-" method for c("matrix", "named").
|-----------------------------------------------------------------------|
| Chuck Taylor Senior Quality Assurance Engineer |
| MathSoft Seattle, WA, USA chuck@statsci.com |
| |
| "All statements in this message represent the opinions of the author |
| and do not necessarily reflect official MathSoft policy or position." |
|-----------------------------------------------------------------------|
--------------BB1E09CEFE0F1D15F7524DCF--
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news