Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:40:28+00:00 2026-06-11T03:40:28+00:00

The documentation says vapply is similar to sapply , but has a pre-specified type

  • 0

The documentation says

vapply is similar to sapply, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:40:30+00:00Added an answer on June 11, 2026 at 3:40 am

    As has already been noted, vapply does two things:

    • Slight speed improvement
    • Improves consistency by providing limited return type checks.

    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 sapply followed by stopifnot to make sure that the return values are consistent with what you expected, but vapply is a little easier (if more limited, since custom error checking code could check for values within bounds, etc.).

    Here’s an example of vapply ensuring your result is as expected. This parallels something I was just working on while PDF scraping, where findD would use a regex to match a pattern in raw text data (e.g. I’d have a list that was split by 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).

    > input1 <- list( letters[1:5], letters[3:12], letters[c(5,2,4,7,1)] )
    > input2 <- list( letters[1:5], letters[3:12], letters[c(2,5,4,7,15,4)] )
    > findD <- function(x) x[x=="d"]
    > sapply(input1, findD )
    [1] "d" "d" "d"
    > sapply(input2, findD )
    [[1]]
    [1] "d"
    
    [[2]]
    [1] "d"
    
    [[3]]
    [1] "d" "d"
    
    > vapply(input1, findD, "" )
    [1] "d" "d" "d"
    > vapply(input2, findD, "" )
    Error in vapply(input2, findD, "") : values must be length 1,
     but FUN(X[[3]]) result is length 2
    

    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, sapply will always return an empty list, regardless of the input type. Compare:

    sapply(1:5, identity)
    ## [1] 1 2 3 4 5
    sapply(integer(), identity)
    ## list()    
    vapply(1:5, identity, integer(1))
    ## [1] 1 2 3 4 5
    vapply(integer(), identity, integer(1))
    ## integer(0)
    

    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

    vapply can be a bit faster because it already knows what format it should be expecting the results in.

    input1.long <- rep(input1,10000)
    
    library(microbenchmark)
    m <- microbenchmark(
      sapply(input1.long, findD ),
      vapply(input1.long, findD, "" )
    )
    library(ggplot2)
    library(taRifx) # autoplot.microbenchmark is moving to the microbenchmark package in the next release so this should be unnecessary soon
    autoplot(m)
    

    autoplot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The documentation says to use: $(input[type='checkbox']).bind( change, function(event, ui) { But I want to
The documentation says, I can do: Return Value: The actual cached response to store
The documentation says to use 1.6.4, but we're up to 1.7.2 now. Can I
The documentation says you can use $.noConflict() like this: jQuery.noConflict(); (function($) { $(function() {
As Apple documentation says expectedContentLength of NSURLResponse may not return the value, if the
SimpleIni Documentation says wchar_t is supported but I don't understand how to use it.
Doctrine documentation says you can use public function construct() { ... } as a
Rsync documentation says what it uses delta encoding when appropriate. But I can't find
The documentation says that... You can use the ToLocalTime method to restore a local
The documentation says that execute() must be called from a UI thread. But, since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.