Hi all, i would like to find the best way to resolve the following problem. Suppoose i have a vector x of length N with k different elements. length(x)=N u<-unique(x) length(u)=k I would like to get a matrix M with k rows and N columns such that: in each line i (i=1,...,k), which(x%in%u[i]) is equal to 1 and 0 else. Thanks for your help. Olivier -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN phone: (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail:olivier.martin at inrialpes.fr 38334 Saint Ismier cedex -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010704/e33482d5/attachment.html
On Wed, Jul 04, 2001 at 01:49:50PM +0200, Olivier Martin wrote:> i would like to find the best way to resolve the following problem. > Suppoose i have a vector x of length N with k different elements. > length(x)=N > u<-unique(x) > length(u)=k > > I would like to get a matrix M with k rows and N columns such that: > in each line i (i=1,...,k), which(x%in%u[i]) is equal to 1 and 0 else. >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:> a <- rbinom(10,3,0.5) > a[1] 1 3 2 2 1 2 0 3 1 3> try.me(a)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> a<-c("Chopin","Mozart","Mozart","Debussy") > try.me(a)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:> model.matrix(~as.factor(a)-1)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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> > I would like to get a matrix M with k rows and N columns such that: > in each line i (i=1,...,k), which(x%in%u[i]) is equal to 1 and 0 else.R> x <- c(1,2,3,3,4,4,5,6) R> sapply(unique(x), fun <- function(a) x == a) [,1] [,2] [,3] [,4] [,5] [,6] [1,] TRUE FALSE FALSE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE FALSE FALSE [3,] FALSE FALSE TRUE FALSE FALSE FALSE [4,] FALSE FALSE TRUE FALSE FALSE FALSE [5,] FALSE FALSE FALSE TRUE FALSE FALSE [6,] FALSE FALSE FALSE TRUE FALSE FALSE [7,] FALSE FALSE FALSE FALSE TRUE FALSE [8,] FALSE FALSE FALSE FALSE FALSE TRUE which is as good as 0, 1 ;-) Torsten> Thanks for your help. > Olivier > > > -- > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > Olivier MARTIN phone: (33) 04 76 61 53 55 > Projet IS2 06 08 67 93 42 > INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 > 655, Av. de l'Europe > Montbonnot e-mail:olivier.martin at inrialpes.fr > 38334 Saint Ismier cedex > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 4 Jul 2001, Olivier Martin wrote:> Hi all, > > i would like to find the best way to resolve the following problem. > Suppoose i have a vector x of length N with k different elements. > length(x)=N > u<-unique(x) > length(u)=k > > I would like to get a matrix M with k rows and N columns such that: > in each line i (i=1,...,k), which(x%in%u[i]) is equal to 1 and 0 else.Another solution outer(unique(x),x,"==")+0 (the +0 just converts it to numeric rather than TRUE/FALSE) -thomas Thomas Lumley Asst. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._