Hi, Is there any function to replace colProds that finds column-wise products of a matrix? Or is there any other function that would give a better solution this problem ? I have a matrix and an array, example:> aGSM1.CEL GSM2.CEL GSM1.CEL 10000_at 1 3 1 10001_at 3 3 3> b10000_at 10001_at 1 3 I want to find the number of occurrences of this array within the matrix (which in this case is 2).> a==bgives me: GSM1.CEL GSM2.CEL GSM1.CEL 10000_at TRUE FALSE TRUE 10001_at TRUE TRUE TRUE What I have done is :> colProds(a==b)GSM1.CEL GSM2.CEL GSM1.CEL 1 0 1> sum(colProds(a==b))2 -----> that gives the value of 2 that I want. The problem is: if the matrix "a" is actually a vector (matrix 1xN), the function "colProds" multiply the elements of the row as if it was a column:> a10000_at 1> bGSM1.CEL GSM2.CEL GSM1.CEL 1 3 1> a==bGSM1.CEL GSM2.CEL GSM1.CEL TRUE FALSE TRUE> colProds(a==b)[1] 0 I could use a if length(b==1) find sum(a==b) instead of sum(colProds(a==b)), but I would have to use this condition many time through the code, so I would like to know if there a better way to do that. Thank you very much. Pablo. -- View this message in context: http://r.789695.n4.nabble.com/Rows-index-colProds-tp2325771p2325771.html Sent from the R help mailing list archive at Nabble.com.
What is your sessionInfo()? /Henrik On Sun, Aug 15, 2010 at 10:47 AM, pablo.andrade <pablo.andrade at gmail.com> wrote:> > Hi, > > Is there any function to replace colProds that finds column-wise products of > a matrix? > > Or is there any other function that would give a better solution this > problem ? > > I have a matrix and an array, example: > >> a > ? ? ? ? ? ? ? ? ? ? GSM1.CEL ?GSM2.CEL ?GSM1.CEL > 10000_at ? ? ? ? ? ? ? 1 ? ? ? ? ? ? 3 ? ? ? ? ? ? 1 > 10001_at ? ? ? ? ? ? ? 3 ? ? ? ? ? ? 3 ? ? ? ? ? ? 3 > >> b > 10000_at 10001_at > ? ? ? 1 ? ? ? ?3 > > I want to find the number of occurrences of this array within the matrix > (which in this case is 2). > >> a==b > gives me: > ? ? ? ? ? ? ? ? ? ? GSM1.CEL ?GSM2.CEL ?GSM1.CEL > 10000_at ? ? ? ? ?TRUE ? ? ? ? FALSE ? ? ? ? ?TRUE > 10001_at ? ? ? ? ?TRUE ? ? ? ? ?TRUE ? ? ? ? ?TRUE > > What I have done is : > >> colProds(a==b) > ? ? ? ? ? ? ? ? ? ? GSM1.CEL ?GSM2.CEL ?GSM1.CEL > ? ? ? ? ? ? ? ? ? ? ? ? ? ?1 ? ? ? ? ? 0 ? ? ? ? ? ? 1 > >> sum(colProds(a==b)) > 2 ? ? ? ? ? ? ? ? ? ?-----> that gives the value of 2 that I want. > > The problem is: if the matrix "a" is actually a vector (matrix 1xN), the > function "colProds" multiply the elements of the row as if it was a column: > >> a > 10000_at > ? ? ? 1 >> b > ? ? ? GSM1.CEL ?GSM2.CEL ?GSM1.CEL > ? ? ? ? ? ?1 ? ? ? ? ? ? 3 ? ? ? ? ? ? 1 >> a==b > > ? ? ? GSM1.CEL ?GSM2.CEL ?GSM1.CEL > ? ? ? ? TRUE ? ? ? ? FALSE ? ? ? ? ?TRUE > >> colProds(a==b) > [1] 0 > > > I could use a if length(b==1) find sum(a==b) instead of sum(colProds(a==b)), > but I would have to use this condition many time through the code, so I > would like to know if there a better way to do that. > > Thank you very much. > Pablo. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Rows-index-colProds-tp2325771p2325771.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
On Aug 15, 2010, at 4:47 AM, pablo.andrade wrote:> > Hi, > > Is there any function to replace colProds that finds column-wise > products of > a matrix? > > Or is there any other function that would give a better solution this > problem ? > > I have a matrix and an array, example: > >> a > GSM1.CEL GSM2.CEL GSM1.CEL > 10000_at 1 3 1 > 10001_at 3 3 3 > >> b > 10000_at 10001_at > 1 3 > > I want to find the number of occurrences of this array within the > matrix > (which in this case is 2). > >> a==b > gives me: > GSM1.CEL GSM2.CEL GSM1.CEL > 10000_at TRUE FALSE TRUE > 10001_at TRUE TRUE TRUE > > What I have done is : > >> colProds(a==b) > GSM1.CEL GSM2.CEL GSM1.CEL > 1 0 1 > >> sum(colProds(a==b)) > 2 -----> that gives the value of 2 that I want. > > The problem is: if the matrix "a" is actually a vector (matrix 1xN),This is a theory untested in the absence of a reproducible example that reproduces the faulty behavior. You may want to look at what is offered by the help page: ?"[" ... especially the drop argument. You can prevent single column matrices from being "vectorized" by using drop = FALSE.> the > function "colProds" multiply the elements of the row as if it was a > column: > >> a > 10000_at > 1 >> b > GSM1.CEL GSM2.CEL GSM1.CEL > 1 3 1 >> a==b > > GSM1.CEL GSM2.CEL GSM1.CEL > TRUE FALSE TRUE > >> colProds(a==b) > [1] 0 > > > I could use a if length(b==1) find sum(a==b) instead of > sum(colProds(a==b)), > but I would have to use this condition many time through the code, > so I > would like to know if there a better way to do that. > > Thank you very much. > Pablo. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Rows-index-colProds-tp2325771p2325771.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi Henrik, My sessionInfo() is: R version 2.10.1 (2009-12-14) i386-pc-mingw32 locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] bioDist_1.18.0 KernSmooth_2.23-3 ygs98cdf_2.5.0 affy_1.24.2 [5] Biobase_2.6.1 loaded via a namespace (and not attached): [1] affyio_1.14.0 preprocessCore_1.8.0 tools_2.10.1 David, the use of "drop=FALSE" seems to solve the problem> a = x[1,,drop=FALSE] > aGSM1.CEL GSM2.CEL GSM3.CEL 10000_at 3 3 1 | -----> Keep the matrix (does not decrease the dimension)> b = 3 > a==bGSM1.CEL GSM2.CEL GSM3.CEL 10000_at TRUE TRUE FALSE> colProds(a==b)GSM1.CEL GSM2.CEL GSM3.CEL 1 1 0> sum(colProds(a==b))[1] 2 Thank you very much for both replies. Pablo. -- View this message in context: http://r.789695.n4.nabble.com/Rows-index-colProds-tp2325771p2329337.html Sent from the R help mailing list archive at Nabble.com.