Dear Petr, Your codes are so beautiful! When I have a matrix s, with each column represents a sequence. I want to recover all equivalent sequences from the sequences/columns in s. I used this command do.call(cbind,apply(s,2,function(x) getEquivalent(x,tt))) This did a good job when ncol(s) >1, but when ncol(s)=1, there is an error. How to getter a better coding which could deal with either the case of ncol(s) >1 or ncol(s)=1 by itself? Thanks a lot, Wei
________________________________ From: Petr Savicky <savi...@cs.cas.cz> To: r-help@r-project.org Sent: Monday, February 13, 2012 5:38 PM Subject: Re: [R] non-isomorphic sequences On Mon, Feb 13, 2012 at 02:04:51PM -0800, zheng wei wrote: > Dear Petr, > > This is fantastic! > > I have one more question, when p=4, tt=4. We have 15 non-isomorphic sequences > as you have generated. Among these 15, I selected 2 sequences. How do I > recover all the members of the equivalent classes corresponding to these 2 > sequences? For example, corresponding to the sequence of 1111, I would like > to recover 1111,2222,3333,4444 from this sequence. Dear Wei: Try the following. getEquivalent <- function(a, tt) { b <- as.matrix(rev(expand.grid(rep(list(1:tt), times=max(a))))) ok <- apply(b, 1, function(x) length(unique(x))) == ncol(b) b <- b[ok, , drop=FALSE] dimnames(b) <- NULL t(apply(b, 1, function(x) x[a])) } getEquivalent(c(1, 1, 1, 1), 4) [,1] [,2] [,3] [,4] [1,] 1 1 1 1 [2,] 2 2 2 2 [3,] 3 3 3 3 [4,] 4 4 4 4 getEquivalent(c(1, 1, 1, 2), 4) [,1] [,2] [,3] [,4] [1,] 1 1 1 2 [2,] 1 1 1 3 [3,] 1 1 1 4 [4,] 2 2 2 1 [5,] 2 2 2 3 [6,] 2 2 2 4 [7,] 3 3 3 1 [8,] 3 3 3 2 [9,] 3 3 3 4 [10,] 4 4 4 1 [11,] 4 4 4 2 [12,] 4 4 4 3 Hope this helps. Petr. ______________________________________________ R-help@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. [[alternative HTML version deleted]]
______________________________________________ R-help@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.