Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Assign mean of values if id is not unique to respective vector index

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].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading