ave                  package:stats                  R Documentation

_G_r_o_u_p _A_v_e_r_a_g_e_s _O_v_e_r _L_e_v_e_l _C_o_m_b_i_n_a_t_i_o_n_s _o_f _F_a_c_t_o_r_s

_D_e_s_c_r_i_p_t_i_o_n:

     Subsets of 'x[]' are averaged, where each subset consist of those
     observations with the same factor levels.

_U_s_a_g_e:

     ave(x, ..., FUN = mean)

_A_r_g_u_m_e_n_t_s:

       x: A numeric.

     ...: Grouping variables, typically factors, all of the same
          'length' as 'x'.

     FUN: Function to apply for each factor level combination.

_V_a_l_u_e:

     A numeric vector, say 'y' of length 'length(x)'. If '...' is
     'g1,g2', e.g., 'y[i]' is equal to 'FUN(x[j]', for all 'j' with
     'g1[j] == g1[i]' and 'g2[j] == g2[i])'.

_S_e_e _A_l_s_o:

     'mean', 'median'.

_E_x_a_m_p_l_e_s:

     require(graphics)

     ave(1:3)# no grouping -> grand mean

     attach(warpbreaks)
     ave(breaks, wool)
     ave(breaks, tension)
     ave(breaks, tension, FUN = function(x)mean(x, trim=.1))
     plot(breaks, main =
          "ave( Warpbreaks )  for   wool  x  tension  combinations")
     lines(ave(breaks, wool, tension            ), type='s', col = "blue")
     lines(ave(breaks, wool, tension, FUN=median), type='s', col = "green")
     legend(40,70, c("mean","median"), lty=1,col=c("blue","green"), bg="gray90")
     detach()

