'An efficient way to expand R vector

In R, I have a vector of indices (A) and values (B). What is an efficient (preferably parallel) approach in R to expand A and B to C, where C is the values in B located in the index of A?

Example:

A = c(4, 7, 11, 20)

B = c(11, 14, 22, 3)

C = c(0,0,0,11,0,0,14,0,0,0,22,0,0,0,0,0,0,0,0,3)

The Brute-force approach is a for-loop which is not what I am looking for.

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source