ftable                 package:base                 R Documentation

_F_l_a_t _C_o_n_t_i_n_g_e_n_c_y _T_a_b_l_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     Create and manipulate ``flat'' contingency tables.

_U_s_a_g_e:

     ftable(..., exclude = c(NA, NaN), row.vars = NULL, col.vars = NULL)
     ftable2table(x)

_A_r_g_u_m_e_n_t_s:

     ...: R objects which can be interpreted as factors (including
          character strings), or a list (or data frame) whose
          components can be so interpreted, or a contingency table
          object of class `"table"' or `"ftable"'.

 exclude: values to use in the exclude argument of `factor' when
          interpreting non-factor objects.

row.vars: a vector of integers giving the numbers of the variables, or
          a character vector giving the names of the variables to be
          used for the rows of the flat contingency table.

col.vars: a vector of integers giving the numbers of the variables, or
          a character vector giving the names of the variables to be
          used for the columns of the flat contingency table.

       x: an arbitrary R object.

_D_e_t_a_i_l_s:

     `ftable' creates ``flat'' contingency tables.  Similar to the
     usual contingency tables, these contain the counts of each
     combination of the levels of the variables (factors) involved. 
     This information is then re-arranged as a matrix whose rows and
     columns correspond to unique combinations of the levels of the row
     and column variables (as specified by `row.vars' and `col.vars',
     respectively).  The combinations are created by looping over the
     variables in reverse order (so that the levels of the
     ``left-most'' variable vary the slowest).  Displaying a
     contingency table in this flat matrix form (via `print.ftable',
     the print method for objects of class `"ftable"') is often
     preferable to showing it as a higher-dimensional array.

     `ftable' is a generic function.  Its default method,
     `ftable.default', first creates a contingency table in array form
     from all arguments except `row.vars' and `col.vars'. If the first
     argument is of class `"table"', it represents a contingency table
     and is used as is; if it is a flat table of class `"ftable"', the
     information it contains is converted to the usual array
     representation using `ftable2table'.  Otherwise, the arguments
     should be R objects which can be interpreted as factors (including
     character strings), or a list (or data frame) whose components can
     be so interpreted, which are cross-tabulated using `table'.  Then,
     the arguments `row.vars' and `col.vars' are used to collapse the
     contingency table into flat form.  If neither of these two is
     given, the last variable is used for the columns.  If both are
     given and their union is a proper subset of all variables
     involved, the other variables are summed out.

     Function `ftable.formula' provides a formula method for creating
     flat contingency tables.

     `ftable2table' converts a contingency table in flat matrix form to
     one in standard array form.

_V_a_l_u_e:

     `ftable' returns an object of class `"ftable"', which is a matrix
     with counts of each combination of the levels of variables with
     information on the names and levels of the (row and columns)
     variables stored as attributes `"row.vars"' and `"col.vars"'.

_S_e_e _A_l_s_o:

     `ftable.formula' for the formula interface (which allows a `data =
     .' argument); `table' for ``ordinary'' cross-tabulation.

_E_x_a_m_p_l_e_s:

     ## Start with a contingency table.
     data(Titanic)
     ftable(Titanic, row.vars = 1:3)
     ftable(Titanic, row.vars = 1:2, col.vars = "Survived")
     ftable(Titanic, row.vars = 2:1, col.vars = "Survived")

     ## Start with a data frame.
     data(mtcars)
     x <- ftable(mtcars[c("cyl", "vs", "am", "gear")])
     x
     ftable(x, row.vars = c(2, 4))

