[R] non-isomorphic sequences

Petr Savicky savicky at cs.cas.cz
Tue Feb 14 09:22:39 CET 2012


Dear Wei:

> 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?

It is not clear, which error you get. After adapting the code
to generate sequences in columns instead of rows, i get for
a single column s the error

  Error in do.call(cbind, apply(s, 2, function(x) getEquivalent(x, tt))) : 
  second argument must be a list

This error is caused by the fact that apply() produces preferably
an array and produces a list only if an array canot be used.
For "do.call", we always need a list. A possible solution is
as follows.

  check.row <- function(x)
  {
      y <- unique(x)
      all(y == seq.int(along=y))
  }

  p <- 3
  tt <- 3
  elem <- lapply(as.list(pmin(1:p, tt)), function(x) seq.int(length=x))
  s <- as.matrix(rev(expand.grid(rev(elem))))
  ok <- apply(s, 1, check.row)
  s <- t(s[ok, ]) # sequences are in columns

  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
      apply(b, 1, function(x) x[a]) # sequences are in columns
  }

  reduced <- s[, 1, drop=FALSE]
  seqList <- lapply(apply(reduced, 2, FUN=list), unlist)
  do.call(cbind, lapply(seqList, function(x) getEquivalent(x, tt)))

       [,1] [,2] [,3]
  [1,]    1    2    3
  [2,]    1    2    3
  [3,]    1    2    3

Hope this helps.

All the best, Petr.



More information about the R-help mailing list