split                  package:base                  R Documentation

_D_i_v_i_d_e _i_n_t_o _G_r_o_u_p_s _a_n_d _R_e_a_s_s_e_m_b_l_e

_D_e_s_c_r_i_p_t_i_o_n:

     'split' divides the data in the vector 'x' into the groups defined
     by 'f'.  The replacement forms replace values corresponding to
     such a division.  'unsplit' reverses the effect of 'split'.

_U_s_a_g_e:

     split(x, f, drop = FALSE, ...)
     split(x, f, drop = FALSE, ...) <- value
     unsplit(value, f, drop = FALSE)

_A_r_g_u_m_e_n_t_s:

       x: vector or data frame containing values to be divided into
          groups.

       f: a 'factor' in the sense that 'as.factor(f)' defines the
          grouping, or a list of such factors in which case their
          interaction is used for the grouping.

    drop: logical indicating if levels that do not occur should be
          dropped (if 'f' is a 'factor' or a list).

   value: a list of vectors or data frames compatible with a splitting
          of 'x'. Recycling applies if the lengths do not match.

     ...: further potential arguments passed to methods.

_D_e_t_a_i_l_s:

     'split' and 'split<-' are generic functions with default and
     'data.frame' methods. The data frame method can also be used to
     split a matrix into a list of matrices, and the replacement form
     likewise, provided they are invoked explicitly.

     'unsplit' works with lists of vectors or data frames (assumed to
     have compatible structure, as if created by 'split'). It puts
     elements or rows back in the positions given by 'f'. In the data
     frame case, row names are obtained by unsplitting the row name
     vectors from the elements of 'value'.

     'f' is recycled as necessary and if the length of 'x' is not a
     multiple of the length of 'f' a warning is printed.

     Any missing values in 'f' are dropped together with the
     corresponding values of 'x'.

_V_a_l_u_e:

     The value returned from 'split' is a list of vectors containing
     the values for the groups.  The components of the list are named
     by the _used_ factor levels given by 'f'.  (If 'f' is longer than
     'x' then some of the components will be of zero length.)

     The replacement forms return their right hand side.  'unsplit'
     returns a vector or data frame for which 'split(x, f)' equals
     'value'

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'cut'

_E_x_a_m_p_l_e_s:

     require(stats); require(graphics)
     n <- 10; nn <- 100
     g <- factor(round(n * stats::runif(n * nn)))
     x <- rnorm(n * nn) + sqrt(as.numeric(g))
     xg <- split(x, g)
     boxplot(xg, col = "lavender", notch = TRUE, varwidth = TRUE)
     sapply(xg, length)
     sapply(xg, mean)

     ### Calculate z-scores by group

     z <- unsplit(lapply(split(x, g), scale), g)
     tapply(z, g, mean)

     # or

     z <- x
     split(z, g) <- lapply(split(x, g), scale)
     tapply(z, g, sd)

     ### data frame variation

     ## Notice that assignment form is not used since a variable is being added

     g <- airquality$Month
     l <- split(airquality, g)
     l <- lapply(l, transform, Oz.Z = scale(Ozone))
     aq2 <- unsplit(l, g)
     head(aq2)
     with(aq2, tapply(Oz.Z,  Month, sd, na.rm=TRUE))
      

     ### Split a matrix into a list by columns
     ma <- cbind(x = 1:10, y = (-4:5)^2)
     split(ma, col(ma))

     split(1:10, 1:2)

