[R] dataset index
jim holtman
jholtman at gmail.com
Thu Dec 3 23:28:49 CET 2009
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?
More information about the R-help
mailing list