[R] help with matrix

From: Yingchen Wang <riddle_chen94_at_yahoo.com>
Date: Sun, 30 Dec 2007 19:52:52 -0800 (PST)


Hi, dear all:

   I am a beginner. I appreciate any help or hint from you.    I am trying to do calculation with matrices. I have 3 matrices. One is matrixA, 2nd is matrixB, and last is matrixC.     Here is matrixA:

       1.8511.40.0831.001

0.8771.30.1161.33
1.9021.21.1020.302
0.8640.1261.110.252
1.8230.2161.0020.307

Next is matrixB:
     0.8761.770.1930.328
0.8911.0090.2381.004
0.8641.1150.2760.22

0.8871.3060.1660.239
0.8521.0011.0080.251

Last is matrixC:

 0.5  1.0   0.5  1.0
1.0   1.0  1.0   0.5
1.0   1.0  1.0   0.5
1.0   1.0  1.0   0.5
0.5   0.5  1.0   0.5

What I want is to match both matrixA and matrixB with matrixC. Ignore the 1st column in matrixA and matrixB. When the element im matrixC is 1.0, multilply the corresponding element in matrixA by the corresponding element in matrixB. Otherwise, ignore it. I wrote the following code with some error in the logic:   

A<-read.table('matrixA.txt', header=F)
B<-read.table('matrixB.txt', header=F)
C<-read.table('matrixC.txt', header=F)

a1<-as.matrix(A[,1])
arest<-as.matrix(A[,2:5])
b1<-as.matrix(B[,1])
brest<-as.matrix(B[,2:5])
C<-as.matrix(C)
if(C= =1){
ra<-(arest*brest)
}else {

throw<-(arest*brest)
}

ra

I got a warning message:
Warning message:the condition has length > 1 and only the first element will be used in: if.....

When I changed the if part to:
 ifelse (C= =1,ra<-(arest*brest),throw<-(arest*brest))    

The result is the multiplication of each element in matrixA and matrixB.

Anyone has an idea about what is wrong. Thanks

Riddle



Be a better friend, newshound, and

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 31 Dec 2007 - 03:56:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 31 Dec 2007 - 05:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.