Re: [Rd] sapply improvements

From: Duncan Murdoch <>
Date: Thu, 12 Nov 2009 09:35:53 -0500

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


      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 mailing list Received on Thu 12 Nov 2009 - 14:38:24 GMT

This archive was generated by hypermail 2.2.0 : Thu 12 Nov 2009 - 17:40:23 GMT