For the following example, I am looking for an efficient solution. In base R only.
# toy data/example
idx <- c(1,2,3,3,4)
vals <- c(3,6,7,1,5)
res <- rep(NA, length = 10)
res[idx] <- vals
# gives
res[idx]
#> [1] 3 6 1 1 5
What I am aiming for instead:
# desired output
res[idx]
[1] 3 6 4 4 5
E.g. if idx is not unique (the case for $idx=3$), I would like to store the mean of $7+1$ instead of $1$ [last evaluated value].
Note, in the real application $idx=3$ may occur several times. Also, there can be thousands of non-unique indices/values in idx.
>Solution :
You can use aggregate to get the mean per idx.
. <- aggregate(vals ~ idx, FUN=mean)
res[.$idx] <- .$vals
res[idx]
#[1] 3 6 4 4 5