How to vectorize this process in R without using too many loops ?
I have this function :
HM=function(CO,CS,CD,CSD){
if(CO-CS)>1){
return(2^(CS)/(2^(CO)-2^(CSD)))
}
else if(CO-CD)>1){
return(1-2^(CD)/(2^(CO)-2^(CSD)))
}
return(0)
}
Basically I need to get HM value for every combination of {CO,CS,CD,CSD} over thoses values :
CO 25.76031685 25.71126747 25.90163231
CS 24.40528297 24.09929848 23.51999092
CD 25.99405861 25.72906113 25.61374474
CSD 35.94195557 36.07263184 34.00024414
So I need to get those values :
HM(25.76031685,24.40528297,25.99405861,35.94195557)
HM(25.71126747,24.40528297,25.99405861,35.94195557)
HM(25.90163231,24.40528297,25.99405861,35.94195557)
HM(25.76031685,24.09929848,25.99405861,35.94195557)
HM(25.71126747,24.09929848,25.99405861,35.94195557)
HM(25.90163231,24.09929848,25.99405861,35.94195557)
HM(25.76031685,23.51999092,25.99405861,35.94195557)
HM(25.71126747,23.51999092,25.99405861,35.94195557)
HM(25.90163231,23.51999092,25.99405861,35.94195557)
etc...
Basically it's all the combination with 4 vectors of 3 elements :
Vectors :
a=c(1,2,3)
b=c(1,2,3)
c=c(1,2,3)
d=c(1,2,3)
Combinations :
1,1,1,1
2,1,1,1
1,2,1,1
1,1,2,1
1,1,1,2
3,1,1,1
1,3,1,1
etc...
I'm not sure how to count the number of combinations. Of course I could use 4 nested loops but I want to learn how to do it with vectorization since R is too slow for loops. I think we can use expand.grid but I don't know how. Also the table is in excel, I can export it in .csv but I'm not sure of the best way to implement this stuff so thank's for your help !