Hello, All, I have a dataset that looks like this: x <- matrix(c( 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 2, 1, 1, 2, 1, 1, 3, 1, 1, 3, 1, 1, 3, 1), ncol = 5, byrow = T, dimnames = list(1:10, c("gender", "race", "disease"))) I want to write a function to produce several matrices including only ?TRUE? and ?FALSE? for the different levels of the variables (these matrices may be thought as index matrices), like> m1TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> m2FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE> m3FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE> m4FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE Can anyone please help how to get this done? Your help would be greatly appreciated. Lisa -- View this message in context: http://n4.nabble.com/dataset-index-tp948049p948049.html Sent from the R help mailing list archive at Nabble.com.
Does this do what you want:> x <- matrix(c(+ 0, 0, 0, + 0, 0, 0, + 0, 1, 0, + 0, 1, 0, + 0, 1, 0, + 1, 2, 1, + 1, 2, 1, + 1, 3, 1, + 1, 3, 1, + 1, 3, 1), + ncol = 3, byrow = T, + dimnames = list(1:10, c("gender", "race", "disease")))> key <- apply(x, 1, paste, collapse=":") > m.flags <- lapply(unique(key), function(.indx){+ key == .indx + })> # create the keys > do.call(rbind, m.flags)1 2 3 4 5 6 7 8 9 10 [1,] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [2,] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE [3,] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE>On Thu, Dec 3, 2009 at 5:07 PM, Lisa <lisajca at gmail.com> wrote:> > Hello, All, > > I have a dataset that looks like this: > > x <- matrix(c( > 0, 0, 0, > 0, 0, 0, > 0, 1, 0, > 0, 1, 0, > 0, 1, 0, > 1, 2, 1, > 1, 2, 1, > 1, 3, 1, > 1, 3, 1, > 1, 3, 1), > ncol = 5, byrow = T, > dimnames = list(1:10, c("gender", "race", "disease"))) > > I want to write a function to produce several matrices including only ?TRUE? > and ?FALSE? for the different levels of the variables (these matrices may be > thought as index matrices), like > >> m1 > TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > >> m2 > FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE > >> m3 > FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE > >> m4 > FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE > > Can anyone please help how to get this done? Your help would be greatly > appreciated. > > Lisa > > -- > View this message in context: http://n4.nabble.com/dataset-index-tp948049p948049.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Thu, 3 Dec 2009, Lisa wrote:> > Hello, All, > > I have a dataset that looks like this: > > x <- matrix(c( > 0, 0, 0, > 0, 0, 0, > 0, 1, 0, > 0, 1, 0, > 0, 1, 0, > 1, 2, 1, > 1, 2, 1, > 1, 3, 1, > 1, 3, 1, > 1, 3, 1), > ncol = 5, byrow = T, > dimnames = list(1:10, c("gender", "race", "disease"))) > > I want to write a function to produce several matrices including only ?TRUE? > and ?FALSE? for the different levels of the variables (these matrices may be > thought as index matrices), like > >> m1 > TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > >> m2 > FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE > >> m3 > FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE > >> m4 > FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE > > Can anyone please help how to get this done? Your help would be greatly > appreciated.Perhaps apply(x , 2, function(x) model.matrix(~0+factor(x))==1) ?? Chuck p.s. ncol = 3, I would think.> > Lisa > > -- > View this message in context: http://n4.nabble.com/dataset-index-tp948049p948049.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Thank you for your help. Your script works very well. Lisa jholtman wrote:> > Does this do what you want: > >> x <- matrix(c( > + 0, 0, 0, > + 0, 0, 0, > + 0, 1, 0, > + 0, 1, 0, > + 0, 1, 0, > + 1, 2, 1, > + 1, 2, 1, > + 1, 3, 1, > + 1, 3, 1, > + 1, 3, 1), > + ncol = 3, byrow = T, > + dimnames = list(1:10, c("gender", "race", "disease"))) >> key <- apply(x, 1, paste, collapse=":") >> m.flags <- lapply(unique(key), function(.indx){ > + key == .indx > + }) >> # create the keys >> do.call(rbind, m.flags) > 1 2 3 4 5 6 7 8 9 10 > [1,] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE > [2,] FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE > [3,] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE > [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE >> > > > On Thu, Dec 3, 2009 at 5:07 PM, Lisa <lisajca at gmail.com> wrote: >> >> Hello, All, >> >> I have a dataset that looks like this: >> >> x <- matrix(c( >> 0, 0, 0, >> 0, 0, 0, >> 0, 1, 0, >> 0, 1, 0, >> 0, 1, 0, >> 1, 2, 1, >> 1, 2, 1, >> 1, 3, 1, >> 1, 3, 1, >> 1, 3, 1), >> ncol = 5, byrow = T, >> dimnames = list(1:10, c("gender", "race", "disease"))) >> >> I want to write a function to produce several matrices including only >> ?TRUE? >> and ?FALSE? for the different levels of the variables (these matrices may >> be >> thought as index matrices), like >> >>> m1 >> TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE >> >>> m2 >> FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE >> >>> m3 >> FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE >> >>> m4 >> FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE >> >> Can anyone please help how to get this done? Your help would be greatly >> appreciated. >> >> Lisa >> >> -- >> View this message in context: >> http://n4.nabble.com/dataset-index-tp948049p948049.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at 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. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://n4.nabble.com/dataset-index-tp948049p948080.html Sent from the R help mailing list archive at Nabble.com.