Hi everyone!!!! I'm having some difficulty to automatized a piece of my code.
I had some functions with different arguments, for example:
ample.e <- function(a,b,c,d){
return(abs((a*(c+d))/(c*(a+b))))
}
cityblock.e <- function(b,c){
return(b+c)
}
inner.product.e <- function(a,d){
return(a+d)
}
Those are similarities measures for categorical (or binary) data. The arguments a, b, c, d, and n are the elements of a contingency table and were previously calculated (each one is a matrix). I need to use those functions to fill a correlation matrix (N x N) that is in another function:
compute.measure <- function(labels, num.labels, a, b, c, d, n, FUN){
retorno = list()
m <- build.matrix.corr(num.labels, labels)
u = (num.labels*num.labels)
pb <- progress_bar$new(total = u)
for (i in 1:num.labels){
for (j in 1:num.labels){
x = as.numeric(a[i,j]) # get the value from the position (i,j) in matrix a
y = as.numeric(b[i,j]) # get the value from the position (i,j) in matrix b
w = as.numeric(c[i,j]) # get the value from the position (i,j) in matrix c
z = as.numeric(d[i,j]) # get the value from the position (i,j) in matrix d
k = as.numeric(n[i,j]) # get the value from the position (i,j) in matrix n
m[i,j] = FUN # here is the problem!!!
pb$tick()
Sys.sleep(1/u)
gc()
} # end intern for
gc()
} # enf extern for
return(m)
gc()
}
Then, I call the function:
# res1 is the result of another function that I call to fill the matrices a,b,d,c, and n.
ma = res1$ma # matrix a
mb = res1$mb # matrix b
mc = res1$mc # matrix c
md = res1$md # matrix d
mn = res2$mn # matrix n
res4 = compute.measure(labels, num.labels, ma, mb, mc, md, mn, cityblock.e)
Here is an example of a result that I will get with the function "compute.measure". For any similarity measure (with different arguments) that I pass as an argument to the function "compute.measure", I will get a correlation matrix N x N as result!
My problem is that I need to use specific arguments in the call of the function: m[i,j] = FUN(arguments). I research a lot yesterday in Google, but it seems that I'm lacking some deep knowledge about advanced R. Indeed, I really don't know everything about R. But, I want to construct a piece of code that I can pass a function with their specific arguments to use in that line to fill the matrix. Is that possible? I really don't want to write a lot of functions, or use a switch/case, because for me I think that is possible to do that in R - I'm wrong? I don't know if I explained my problem in a way that you really can understand, sorry about that.
Sorry for the huge text. But I'm going crazy with that issue! I'm very grateful for any suggestion.