Why You Should Use vapply in R


Visit: TheAutomatic.net


Blogger, TheAutomatic.net, and Senior Data Scientist

In this post we’ll cover the vapply function in R. vapply is generally lesser known than the more popular sapplylapply, and apply functions. However, it is very useful when you know what data type you’re expecting to apply a function to as it helps to prevent silent errors. Because of this, it can be more advisable to use vapply rather than sapply or lapply.


Let’s take the following example. Here, we have a list of numeric vectors and we want to get the max value of each vector. That’s simple enough – we can just use sapply and apply the max function for each vector.

test <- list(a = c(1, 3, 5), b = c(2,4,6), c = c(9,8,7))

sapply(test, max)

But what if our list also contains a vector of characters, rather than numeric values? Running the example below gives us the max value of each vector, but coerces the final result to a character vector, which would not be desirable if our code / script is expecting numeric results.

test$d <- c("a", "b", "c")

sapply(test, max)


To get around this, we can use vapply. In this case, we just need to add an extra parameter that specifies the data type expected for the function being applied – i.e. when we apply the max function we expect to be applying it to numeric vectors. Running the code below will result in an error, rather than coercing the final result to a character vector.

vapply(test, max, numeric(1))

numeric(1) signifies that we just want individual numeric values. numeric(0) would signify we’re expecting zero-length numeric values.

To adjust the expected data type, we just need to change the third parameter. For example, if we are expecting a list of data frames, we could write:

vapply(frames, nrow, data.frame(1))

Alternatively, the third parameter is called FUN.VALUE, so we could also do this:

vapply(frames, nrow, FUN.VALUE = data.frame(1))


That’s all for now! Check out online courses here to learn more about R / Python / data science (including my course on web scraping)! To learn about other lesser-known apply functions, see this post.

Visit  TheAutomatic.net to download the complete R script: http://theautomatic.net/2020/10/20/why-you-should-use-vapply-in-r/

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with permission from TheAutomatic.net. The views expressed in this material are solely those of the author and/or TheAutomatic.net and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.