On Fri, Aug 29, 2008 at 6:46 PM, David Huffer <[EMAIL PROTECTED]> wrote:
> I'm looking for something along the lines of
>
>  which ( table ( x ) == max ( table ( x ) ) )
>
> to find the most common level of one factor
> by several other factors. For instance, I've got

>  > X <- data.frame (
>  +   x = factor ( sample ( c ( "A" , "B" , "C" , "D" ) , 20 , r = T ) )
>  +   , z1 = factor ( sample ( c ( "Before" , "After" ) , 20 , r = T ) )
>  +   , z2 = factor ( sample ( c ( "Red" , "Green" , "Blue" ) , 20 , r =
> T ) )
>  +   , z3 = factor ( sample ( 0:6 , 20 , r = T ) )
>  + )
>  > X
>     x     z1    z2 z3
>  1  D  After  Blue  0
>  2  D Before Green  3
>  3  A Before   Red  5
>  4  C  After Green  6
>  5  C Before Green  6
>  6  C Before Green  0
>  7  C Before   Red  1
>  8  C Before   Red  5
>  9  A Before  Blue  3
>  10 A  After Green  4
>  11 D  After   Red  3
>  12 C  After Green  5
>  13 A  After   Red  0
>  14 B  After   Red  6
>  15 B Before   Red  3
>  16 A Before  Blue  4
>  17 B Before  Blue  5
>  18 A  After  Blue  1
>  19 B Before Green  1
>  20 C Before   Red  2
>  >
> and i would like to be able to say which category of x was the
> most common for each combination of z1, z2, and z3. So, here,
> which category of x was the most common for Before,Red,0;
> Before,Red,1; ... Before,Red,6; Before,Green,0; Before,Green,1;
> ... Before,Green,6;...

> This seems simple rather as i type it out, but i havent been
> able to come up with the right approach so far. its friday night
> so maybe i should just go home and wait until monday...

A general approach is to create the interaction of the factors z1, z2,
z3 then the cross-tabulation of x by this factor then apply which.max
to the columns (or rows, depending on how you do the cross-tabulation)
of the table.  See the (I hope) enclosed.
X <- data.frame(x = factor(sample(LETTERS[1:4], 20 , replace = TRUE)),
                z1 = factor(sample(c("Before" ,"After"), 20, replace = TRUE)),
                z2 = factor(sample(c("Red", "Green", "Blue"),20,
                  replace = TRUE)),
                z3 = factor(sample(0:6, 20, replace = TRUE)))
(X <- within(X, z123 <- (z1:z2:z3)[drop = TRUE]))
(tab <- xtabs(~ x + z123, X))
apply(tab, 2, which.max)
______________________________________________
R-help@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.

Reply via email to