Dear List, i know there are some solutions for this in the archive, but they're not very good for numeric matrices, since they usually convert rows/columns to character strings. Is there an easy way to do $subject for numeric matrices properly, or i need to do it by hand? Thanks, Gabor
Hmmm, maybe i wasn't clear enough. So, i need the number of occurences for each row/column in the matrix. For your matrix the desired output would be something like a matrix 1 2 3 4 5 6 1 3 2 1 1 1 and the number of occurences for each row: 2 2 1 1 I know some solutions like https://stat.ethz.ch/pipermail/r-help/2007-December/149033.html but this is not the best since it converts numeric data to strings, and this has a lot of problems. Btw. i've just noticed that unique.matrix does the same..... I guess i need to code this for myself, just wanted to be sure that i'm not missing the easy way.... Gabor On Fri, Jan 11, 2008 at 02:07:37PM -0500, John Kane wrote:> ?unique & ?length > > #unique rows in a matrix > X<-matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1),6,3,byrow=TRUE) > length(unique(X)[,1]) > > #unique columns in a data.frame > Y <- data.frame( a=1:5, b=2:6, c=1:5) > length(unique(t(Y)[,1])) > > --- Gabor Csardi <csardi at rmki.kfki.hu> wrote: > > > Dear List, > > > > i know there are some solutions for this in the > > archive, > > but they're not very good for numeric matrices, > > since they > > usually convert rows/columns to character strings. > > Is there > > an easy way to do $subject for numeric matrices > > properly, > > or i need to do it by hand? > > > > Thanks, > > Gabor > > > > ______________________________________________ > > 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. > > > > > > Get a sneak peak at messages with a handy reading pane with All new Yahoo! Mail: http://mail.yahoo.ca-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
On Sat, Jan 12, 2008 at 12:35:47PM -0500, John Kane wrote:> I definately did not read it that way but that may > have been my fault. That table approach is quite > nice! > > Using it, you could just rebuild the vectors from the > names. Does this do more or less what you want?John, thanks. Still not good enough. :( The problem is not that the result was in string format, but that not the real values are compared, only the rounded values to six (?) decimals. I know this is only the default and more could be done by setting some parameters (probably options(digits) is enough), but then it is not very efficient, since instead of comparing 8 byte doubles i'll be comparing quite long strings for every single number in the matrix. This seems quite a hack to me. I'm thinking about the following solution. We hash every row/column of the matrix, then sort the hashed values, and compare only those rows/columns for which the hash values are the same. (With the proper comparision, ie. via "==" or all.equal.) Of course i'm not completely sure that this is faster than comparing long strings, but i'll give it a try. I have quite big matrices, that's why i need an efficient solution. (I'm sending this to the list, because someone else was also interested, but i lost his email address.) Gabor> X<-matrix(c(1,2,3,1,2,3,4,5,6,1,3,2,4,5,6,1,1,1),6,3,byrow=TRUE) > xx <-table(apply(X, 1, paste, collapse=",")) > hh <- names(xx) > nnk <-(strsplit(hh, ",")) > kkn <- lapply(nnk, as.numeric) > df1 <-t(as.data.frame(kkn)) > cbind(df1,xx) >[...] -- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM