The documentation says
vapplyis similar tosapply, but has a pre-specified type of return value, so it can be safer […] to use.
Could you please elaborate as to why it is generally safer, maybe providing examples?
P.S.: I know the answer and I already tend to avoid sapply. I just wish there was a nice answer here on SO so I can point my coworkers to it. Please, no “read the manual” answer.
As has already been noted,
vapplydoes two things:The second point is the greater advantage, as it helps catch errors before they happen and leads to more robust code. This return value checking could be done separately by using
sapplyfollowed bystopifnotto make sure that the return values are consistent with what you expected, butvapplyis a little easier (if more limited, since custom error checking code could check for values within bounds, etc.).Here’s an example of
vapplyensuring your result is as expected. This parallels something I was just working on while PDF scraping, wherefindDwould use a regex to match a pattern in raw text data (e.g. I’d have a list that wassplitby entity, and a regex to match addresses within each entity. Occasionally the PDF had been converted out-of-order and there would be two addresses for an entity, which caused badness).Because two there are two d’s in the third element of input2, vapply produces an error. But sapply changes the class of the output from a character vector to a list, which could break code downstream.
As I tell my students, part of becoming a programmer is changing your mindset from "errors are annoying" to "errors are my friend."
Zero length inputs
One related point is that if the input length is zero,
sapplywill always return an empty list, regardless of the input type. Compare:With
vapply, you are guaranteed to have a particular type of output, so you don’t need to write extra checks for zero length inputs.Benchmarks
vapplycan be a bit faster because it already knows what format it should be expecting the results in.