[R] Most common level of a factor by

David Huffer David.Huffer at csosa.gov
Sat Aug 30 01:46:49 CEST 2008


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...

--
 David



More information about the R-help mailing list