From murdoch at stats.uwo.ca  Thu Nov 12 15:35:53 2009
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu, 12 Nov 2009 09:35:53 -0500
Subject: [Rd] sapply improvements
In-Reply-To: <77EB52C6DD32BA4D87471DCD70C8D700021320C6@NA-PA-VBE03.na.tibco.com>
References: <20091104160510.1CF24282BE82@mail.pubhealth.ku.dk>	<4AF1B00D.7060809@stats.uwo.ca>	<77EB52C6DD32BA4D87471DCD70C8D70002131F9B@NA-PA-VBE03.na.tibco.com>	<4AF1E2EC.7060405@stats.uwo.ca>
	<77EB52C6DD32BA4D87471DCD70C8D700021320C6@NA-PA-VBE03.na.tibco.com>
Message-ID: <4AFC1D49.9060609@stats.uwo.ca>

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



[Rd] sapply improvements
Duncan Murdoch