0
Personal I am trying to replace my code that has double for by one with double lapply in order to optimize the program since the r works matrix, this functional in this way:
k<-1
for(i in 1:nrow(tab1)){
for(j in 1:nrow(tab2)){
tab3$col1[k]<-tab1$col1[i]
tab3$col2[k]<-tab2$col3[j]
k<-k+1
}
}
I would like it to be like this, but I don’t know how you can help? Thank you
lapply(1:nrow(tab1),fuction(i){
lapply(1:nrow(tab2), function(j){
??????????
})
})
Theoretically, trading for the lapply won’t make any difference. R is only faster if you use vector functions, written in C/C++ otherwise and the others are equivalent.
– Daniel Falbel
Why not
tab3$col1 <- tab1$col1
andtab3$col2 <- tab2$col3
? Note that as your code is, the instructionk <- k + 1
will performnrow(tab1)*nrow(tab2)
times. That’s what you want?– Rui Barradas
The code has several conditional structures that when satisfied record different records on each line, but as the friend said that the change from the is to the lapply does not make difference I will look for another solution because the way it is it takes a long time to read the tables I think I will go for a solution in PYTHON. Thank you
– aguch
I think in python will not get much faster either. Just like in R, loops are inefficient in python. The best solution would be to do in C or C++... Take a look at the Rcpp package.
– Daniel Falbel
Put on Phyton too! Thanks Daniel I’ll leave for C then
– aguch