[R] dataset index

Charles C. Berry cberry at tajo.ucsd.edu
Thu Dec 3 23:34:09 CET 2009


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



More information about the R-help mailing list