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> tmatApple 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 > >
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 302Now 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. >------------------------------------------------------------------------------>
Srinivas Iyyer
2006-Jul-30 15:18 UTC
[R] overlaying the values of tab-delim file in to a pre-existing matrix
Hello : 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. Tab-delim file has smaller values than the matrix. Matrix = tmat tab-delim file read as data.frame = tb My aim is to read in a line in # Apple, S , 21. Find column Apple and row S and fill the value 21. For instance:> tmatApple 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 read as a data.frameV1 V2 V3 1 Apple S 21 2 Apple A 21.6 3 Apple O 43 4 Orange A 45 5 Orange O 64 6 Orange S 32.5 7 Mango M 40.3 8 Mango A 32.6 9 Mango S 24.6 Now I have to fill in the values in tb (V3) into tmat. For instance, (Apple, S) pair value is 21, I want Apple Orange Mango Grape Star A 21.6 0 0 0 0 O 0 0 0 0 0 M 0 0 0 0 0 G 0 0 0 0 0 S 21 0 0 0 0> tbm <- as.matrix(tb) >tmat[cbind(match(tbm[,2],rownames(tmat)),match(tbm[,1],colnames(tmat)))] <-tbm[,3] Error: NAs are not allowed in subscripted assignments I am using R.2.2.1 on a Dell Latutite windows XP with 1GB RAM. Could any one please help me whats wrong with above code. thank you.
Gabor Grothendieck
2006-Jul-30 16:22 UTC
[R] overlaying the values of tab-delim file in to a pre-existing matrix
Please provide reproducible examples (as discussed at end of each posting): Lines <- "Apple S 21.0 Apple A 21.6 Apple O 43.0 Orange A 45.0 Orange O 64.0 Orange S 32.5 Mango M 40.3 Mango A 32.6 Mango S 24.6 " tb <- read.table(textConnection(Lines)) # alternative 1 - create a matrix tmat <- matrix(0, nrow = nlevels(tb$V2), ncol = nlevels(tb$V1), dimnames = list(levels(tb$V2), levels(tb$V1))) tmat[cbind(tb$V2, tb$V1)] <- tb$V3 # alternative 2 - out is a data frame in wide format out <- reshape(tb, dir = "wide", timevar = "V1", idvar = "V2") # fix up rownames and colnames and remove first column rownames(out) <- out[,1] out <- out[,-1] colnames(out) <- sub(".*[.]", "", colnames(out)) out[is.na(out)] <- 0 On 7/30/06, Srinivas Iyyer <srini_iyyer_bio at yahoo.com> wrote:> Hello : > > 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. Tab-delim file has smaller values than the > matrix. > > Matrix = tmat > tab-delim file read as data.frame = tb > > > > My aim is to read in a line in # Apple, S , 21. > Find column Apple and row S and fill the value 21. > > > > > 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 read as a data.frame > V1 V2 V3 > 1 Apple S 21 > 2 Apple A 21.6 > 3 Apple O 43 > 4 Orange A 45 > 5 Orange O 64 > 6 Orange S 32.5 > 7 Mango M 40.3 > 8 Mango A 32.6 > 9 Mango S 24.6 > > > Now I have to fill in the values in tb (V3) into tmat. > > > For instance, (Apple, S) pair value is 21, I want > > > Apple Orange Mango Grape Star > A 21.6 0 0 0 0 > O 0 0 0 0 0 > M 0 0 0 0 0 > G 0 0 0 0 0 > S 21 0 0 0 0 > > > > > > tbm <- as.matrix(tb) > > > tmat[cbind(match(tbm[,2],rownames(tmat)),match(tbm[,1],colnames(tmat)))] > <-tbm[,3] > Error: NAs are not allowed in subscripted assignments > > > I am using R.2.2.1 on a > Dell Latutite windows XP with 1GB RAM. > > Could any one please help me whats wrong with above > code. > > thank you. > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >