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:

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

     data(warpbreaks)
     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()

