Skip to content
Prev 12136 / 398502 Next

a little probleme

On Wed, Jul 04, 2001 at 01:49:50PM +0200, Olivier Martin wrote:
You can try something along the lines of the following 
  small function (sort, colnames and rownames stuff are optional)

  try.me <- function(a)
  {
    n <- length(a)
    u <- sort(unique(a))
    m <- length(u)
    ans <- matrix(0,n,m)
    ans[rep(u,rep(n,m))==a] <- 1
    colnames(ans) <- u
    rownames(ans) <- a
    ans
  }

Example:
[1] 1 3 2 2 1 2 0 3 1 3
0 1 2 3
1 0 1 0 0
3 0 0 0 1
2 0 0 1 0
2 0 0 1 0
1 0 1 0 0
2 0 0 1 0
0 1 0 0 0
3 0 0 0 1
1 0 1 0 0
3 0 0 0 1
Chopin Debussy Mozart
Chopin       1       0      0
Mozart       0       0      1
Mozart       0       0      1
Debussy      0       1      0

or make use of the built-in model.matrix function which is essentially
equivalent to what you ask:
as.factor(a)Chopin as.factor(a)Debussy as.factor(a)Mozart
1                  1                   0                  0
2                  0                   0                  1
3                  0                   0                  1
4                  0                   1                  0
attr(,"assign")
[1] 1 1 1

guido



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Message-ID: <20010704143049.A2809@sirio.stat.unipd.it>
In-Reply-To: <3B4302DD.1C8DED50@inrialpes.fr>; from olivier.martin@inrialpes.fr on Wed, Jul 04, 2001 at 01:49:50PM +0200