Replace double for with double lapply

Asked

Viewed 48 times

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.

  • Why not tab3$col1 <- tab1$col1 and tab3$col2 <- tab2$col3? Note that as your code is, the instruction k <- k + 1 will perform nrow(tab1)*nrow(tab2) times. That’s what you want?

  • 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

  • 2

    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.

  • Put on Phyton too! Thanks Daniel I’ll leave for C then

1 answer

1

Hello!

Actually apply’s can be even slower than loops, if in the loop you take care to preallocate your vector.

As for your problem, it seems you want to make all combinations of tab1$col1 and tab2$col3.

I suggest using expand.grid():

tab3 <- expand.grid(col1 = tab1$col1, col2 = tab2$col3)

The order will be different, but this is easily solved with order().

A hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.