[19118] in s-news-athena
[S] Subtracting a "vector" from a matrix in S-PLUS 5.1
daemon@ATHENA.MIT.EDU (Mette Langaas)
Mon Sep 6 07:04:41 1999
Message-Id: <37D39E39.90463F7B@nr.no>
Date: Mon, 06 Sep 1999 12:58:01 +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>
Cc: "shelp@mathsoft.co.uk" <shelp@mathsoft.co.uk>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Dear S-experts and S-users,
We are running S-PLUS 5.1 UNIX, and have found the following problem
which is NOT present under S-PLUS 3.4 UNIX.
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.
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!
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)
> a
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> am_apply(a,2,mean)
> am
[1] 2 5
> names(am)
NULL
> am.wn_am
> names(am.wn)_c("a1","a2")
> names(am.wn)
[1] "a1" "a2"
> t(a)-am
[,1] [,2] [,3]
[1,] -1 0 1
[2,] -1 0 1
> dim(t(a)-am)
[1] 2 3
> (t(a)-am.wn)
[1] -1 -1 0 0 1 1
> dim(t(a)-am.wn)
NULL
A solution to the above is to do as.vector on am.wn.
> am.won_as.vector(am.wn)
> am.won
[1] 2 5
> is.vector(am.won)
[1] T
> t(a)-am.won
[,1] [,2] [,3]
[1,] -1 0 1
[2,] -1 0 1
We would like comments as soon as possible -- before we go through our
code an insert as.vector everywhere for safety..... A reference to where
this change in behaviour is documented is also highly appreciated.
Please answer directly to mette.langaas@nr.no and we will summarize to
the list.
Best regards,
Magne Aldrin and Mette Langaas
Norwegian Computing Center
-----------------------------------------------------------------------
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