[Rd] sapply improvements

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 12 15:35:53 CET 2009


I've just added a version of the improvements to R-devel.  After some 
discussion, we decided this should be a new function, not just a new arg 
to sapply, and I chose vapply from among the 52 different suggestions 
that were offered.

The usage is

  vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)

and some extracts from the docs say:


      ‘vapply’ is similar to ‘sapply’, but has a pre-specified
      type of return value, so it can be safer (and sometimes faster) to
      use.

...

      Simplification is always done in ‘vapply’.  This function
      checks that all values of ‘FUN’ are compatible with the
      ‘FUN.VALUE’, in that they must have the same length and type.
      (Types may be promoted to a higher type within the ordering
      logical < integer < real < complex, but not demoted.)

...
      ‘vapply’ returns a vector or matrix of type matching the
      ‘FUN.VALUE’. If ‘length(FUN.VALUE) != 1’ a matrix will be
      returned with ‘length(FUN.VALUE)’ rows and ‘length(X)’
      columns, otherwise a vector of the same length as ‘X’.  Names
      of rows in the matrix value are taken from the ‘FUN.VALUE’ if
      it is named, otherwise from the result of the first function call.
      Column names of the matrix value or names of the vector value are
      set from ‘X’ as in ‘sapply’.

Thanks for suggesting this.

Duncan Murdoch



More information about the R-devel mailing list