I have an integer variable res
which stores the sum of of each element from one vector to another vector where the results are kept track. Where length(a) = 10
and length(b) = 10 or 15 or any length > length(a).
a <- 1:10
b <- 1:15
nm <- outer(seq_along(a), seq_along(b), FUN = function(x, y) sprintf('a%d + b%d', x, y))
res <- setNames(c(outer(a,b,`+`)), nm)
res
# a1+b1 a2+b1 a3+b1 a4+b1 a5+b1 ... a6+b15 a7+b15 a8+b15 a9+b15 a10+b15
# 2 3 4 5 6 ... 21 22 23 24 25
How can I find the maximum of each unique pair? Let say a10 + b15 = 25
is the maximum, then in the second iteration, any pair containing a10
or b15
are omitted. This process is repeated until no unique pairs are left.
How can I modify the if
statement in the below function to find the mean of the maximum unique pairing? Or there is another way round?
f1 <- function(x) {
x1 <- max(x)
repeat {
x <- x[!grepl(sub(" \\+ ", "|", names(which.max(x))), names(x))]
x1 <- c(x1, max(x))
if(length(x)==1) break
}
return(list(x, mean(x1)))
}
Note: This question is a follow-up of my previous question.