Here's a simple function (same idea):

f <- function(x){
  lenx <- length(x)
  negx <- sum(x < 0)
  mat <- matrix(1, lenx, choose(lenx,negx))
  for(i in seq_len(choose(lenx, negx))){
    mat[combn(lenx,negx)[, i], i] <- -1
  }
  mat
}

x <- c(-1,-1,-1,1,1,1,1)
f(x)

[combn() is now in utils]

 -Peter Ehlers

Greg Snow wrote:
Here is one quick way using the combinat package:

library(combinat)

tmpfun <- function(x) {
+ tmp <- rep(1,5)
+ tmp[x] <- -1
+ tmp
+ }
combn(5,2, tmpfun)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   -1   -1   -1   -1    1    1    1    1    1     1
[2,]   -1    1    1    1   -1   -1   -1    1    1     1
[3,]    1   -1    1    1   -1    1    1   -1   -1     1
[4,]    1    1   -1    1    1   -1    1   -1    1    -1
[5,]    1    1    1   -1    1    1   -1    1   -1    -1

Of course in this case the tmpfun function needs to be rewritten for each 
vector size, so is not generalizable.


______________________________________________
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.

Reply via email to