Skip to content
Prev 342885 / 398506 Next

Frequencies for a list of vectors

Using vapply instead of sapply or unlist(lapply) here gives you a
little more safety.  vapply insists that you supply a FUN.VALUE
argument that gives a prototype (type and length) of the expected
output of FUN.  It will stop if FUN returns something unexpected.
Compare the following where I misspelled 'collapse'; only vapply
catches the error:
300081  300082  300083  300084  600081  600082  600083  600084  900081  900082
   "1 "    "0 "    "1 "    "0 "    "0 "    "0 "    "1 "    "0 "    "0 "    "0 "
 900083  900084  100007 1300011 1300012
   "1 "    "0 "    "1 "    "0 "    "1 "
$`30008`
[1] "1 " "0 " "1 " "0 "

$`60008`
[1] "0 " "0 " "1 " "0 "

$`90008`
[1] "0 " "0 " "1 " "0 "

$`100007`
[1] "1 "

$`130001`
[1] "0 " "1 "
Error in vapply(marieData, paste, collaps = "", FUN.VALUE = "") :
  values must be length 1,
 but FUN(X[[1]]) result is length 4

vapply(X,FUN,FUN.VALUE) also gives you a better result when length(X)
is 0, meaning that you don't have to write special code to catch that
case.


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Aug 5, 2014 at 1:53 PM, Peter Alspach
<Peter.Alspach at plantandfood.co.nz> wrote: