Could you explain why vapply or sapply are so slow in my R code example just below ?
I tried to simplify my initial code to underline the main problem.
What you see here is exactly why it is important to embrace functional programming, when using R. The built in paste0()-function solves exactly what you are trying to achieve. Therefore, when programming in R knowing and understanding how to apply (pun intended) built in functions are key to speed.
In your case, there is overhead in calling one function multiple times, rather than exploiting vectorisation and calling the function one time with appropriate vectors.
In fact R despite the reputation, is not that slow - Using R wrongly or for the wrong task is slow
I suppose that it depends greatly of how the language is dealing data structures under the hood.
Vectors (and vectorization), and lists seem to be the corner stone data structures for R.
So, idealy we have to learn how these structures are basically transformed by the core functions to get an optimal code.
I think you are caught out in particular by mixing types, and relying on implicit type conversion for your results.
When using non-vectorised solutions you are paying this cost repeatedly. Also the way sapply and vapply may try to add names to your results, is a difference.