[R] Selecting Variables

jim holtman jholtman at gmail.com
Tue Aug 5 16:09:59 CEST 2008


I am not sure where the "Max" comes from, but this might be a start for you:

> x <- read.table(textConnection("ID Exposure_1 Exposure_2 Exposure_3
+ 1        y                 y                y
+ 2        y                 y                -
+ 3        y                 -                 -"), header=TRUE, na.strings='-')
> closeAllConnections()
> require(reshape)
> y <- melt(x, id.var='ID')
> # get rid of NAs
> y <- y[complete.cases(y),]
> y
  ID   variable value
1  1 Exposure_1     y
2  2 Exposure_1     y
3  3 Exposure_1     y
4  1 Exposure_2     y
5  2 Exposure_2     y
7  1 Exposure_3     y
> cbind(Unique=tapply(y$ID, y$ID, length))
  Unique
1      3
2      2
3      1
>


On Tue, Aug 5, 2008 at 9:21 AM, Michael Pearmain <mpearmain at google.com> wrote:
> Hi All,
>
> i have a dataset that i want to dynamically inspect for the number of
> variables that start with "Exposure_"  and then for these count the entries
> across each case i.e
>
> ID Exposure_1 Exposure_2 Exposure_3
> 1        y                 y                y
> 2        y                 y                -
> 3        y                 -                 -
>
> So the corresponding new variables that would be created are
>
> ID Max_Exposure Unique_Exposure
> 1           3                       3
> 2           3                       2
> 3           3                       1
>
> I know this may seem fairly basic but it will give me the starting point to
> develop more advanced things with loop and nat lang
>
> Thanks in advance
>
> Mike
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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