[R] Comparing two matrices [Broadcast]
Srinivas Iyyer
srini_iyyer_bio at yahoo.com
Fri Jul 28 22:21:01 CEST 2006
Hi Andy, Thank you very much for your previous tip.
However, I am running into another problem now.
previously, in my tab-delim file I had only two
columns. now I added another column. and I want the
pairs value to go into the matrix instead of 1.
pair (Apple, S) value 10.
> > > tb # tab- delim file.
> > V1 V2 V3
> > 1 Apple S 10
> > 2 Apple A 12
> > 3 Apple O 34
> > 4 Orange A 34
> > 5 Orange O 59
> > 6 Orange S 30
> > 7 Mango M 395
> > 8 Mango A 495
> > 9 Mango S 302
Now I am trying to do the following:
R> tbm <- as.matrix(tb) # turn it into a character
matrix
R> tmat[cbind(match(tbm[,2],
rownames(tmat)),match(tbm[,1],colnames(tmat)))] <-
tbm[,3]
Error: NAs are not allowed in subscripted assignments
I get the above error.
What is wrong with this. I am using R.2.2.1 on a
Dell Latutite windows XP with 1GB RAM.
Could any one please help me what am I doing wrong.
thank you.
--- "Liaw, Andy" <andy_liaw at merck.com> wrote:
> It might be a bit faster to do matrix indexing:
>
> R> tbm <- as.matrix(tb) # turn it into a character
> matrix
> R> tmat[cbind(match(tbm[,2], rownames(tmat)),
> match(tbm[,1],
> colnames(tmat)))] <- 1
> > tmat
> Apple Orange Mango Grape Star
> A 1 1 1 0 0
> O 1 1 0 0 0
> M 0 0 1 0 0
> G 0 0 0 0 0
> S 1 1 1 0 0
>
> HTH,
> Andy
>
>
> From: Srinivas Iyyer
> >
> > hi:
> >
> > I have matrix with dimensions(200 X 20,000). I
> have another
> > file, a tab-delim file where first column
> variables are row
> > names and second column variables are column
> names.
> >
> >
> > For instance:
> >
> > > tmat
> > Apple Orange Mango Grape Star
> > A 0 0 0 0 0
> > O 0 0 0 0 0
> > M 0 0 0 0 0
> > G 0 0 0 0 0
> > S 0 0 0 0 0
> >
> >
> >
> > > tb # tab- delim file.
> > V1 V2
> > 1 Apple S
> > 2 Apple A
> > 3 Apple O
> > 4 Orange A
> > 5 Orange O
> > 6 Orange S
> > 7 Mango M
> > 8 Mango A
> > 9 Mango S
> >
> >
> > I have to read each line of the 'tb' (tab delim
> file), take
> > the first variable, check if matches any rowname
> of the
> > matrix. Take the second variable of the row in and
> check if
> > it matches any column name. If so, put
> > 1 else leave it.
> >
> >
> > The following is a small piece of code that, I
> felt is a
> > solutions. However, since my original matrix and
> tab-delim
> > file is very very huge, I am not sure if it is
> really doing
> > the correct thing. Could any one please help me if
> I am doing
> > this correct.
> >
> >
> >
> > > for(i in 1:length(tb[,1])){
> > + r = tb[i,1]
> > + c = as.character(tb[i,2])
> > + tmat[rownames(tmat)==c,colnames(tmat)==r] <-1 }
> >
> >
> >
> > > tmat
> > Apple Orange Mango Grape Star
> > A 1 1 1 0 0
> > O 1 1 0 0 0
> > M 0 0 1 0 0
> > G 0 0 0 0 0
> > S 1 1 1 0 0
> >
> >
> >
> > Thanks.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>
>
------------------------------------------------------------------------------
> Notice: This e-mail message, together with any
> attachments, contains information of Merck & Co.,
> Inc. (One Merck Drive, Whitehouse Station, New
> Jersey, USA 08889), and/or its affiliates (which may
> be known outside the United States as Merck Frosst,
> Merck Sharp & Dohme or MSD and in Japan, as Banyu)
> that may be confidential, proprietary copyrighted
> and/or legally privileged. It is intended solely for
> the use of the individual or entity named on this
> message. If you are not the intended recipient, and
> have received this message in error, please notify
> us immediately by reply e-mail and then delete it
> from your system.
>
------------------------------------------------------------------------------
>
More information about the R-help
mailing list