alternative for "for loop"

Hi I have a for loop to append pairs for vector, my problem is that sometimes i have hunderds of thousands of pairs so the for loop takes hours and sometimes days to fill the vector, there is any alternative to improve the solution , I mean an alternative for for loop , here is my code :slight_smile:
'''
for (X in 1:len)
{
a[[1]]<-append(a[[1]],set(pair(B[X,1],B[X,2])))
}
'''
I have the pairs as elements in two columns matrix and len rows
Regards

Hi,

  • Could you provide some sample data please.
  • Also, the set() and pair() function are not base functions, which packages are they from?
  • What is the exact goal of the append? I don't understand the a[[1]] (not defined anywhere)

Thx

Hi
here is simple matrix
from to
1 1 2
2 4 5
3 1 3
4 4 6
5 7 9
6 7 10
now the packages I'm using are sets() and igraph() , the goal from append is to put the pairs in one vector a[[1]] at the end i need to get somthing like this
{ (1, 2), (1, 3), (4, 5), (4, 6), (7, 9), (7, 10)}
to create a[[1]] you can use the following script
a<-vector(mode = "list",length = 1)
thanks in advance

I do not know if this would be faster or serve your needs. A vector of vectors is a list, so I used lapply.

B <- matrix(c(1,4,1,4,7,7,2,5,3,6,9,10), ncol = 2)
B
#>      [,1] [,2]
#> [1,]    1    2
#> [2,]    4    5
#> [3,]    1    3
#> [4,]    4    6
#> [5,]    7    9
#> [6,]    7   10
C <- lapply(as.data.frame(t(B)), FUN = function(x) x)
C
#> $V1
#> [1] 1 2
#> 
#> $V2
#> [1] 4 5
#> 
#> $V3
#> [1] 1 3
#> 
#> $V4
#> [1] 4 6
#> 
#> $V5
#> [1] 7 9
#> 
#> $V6
#> [1]  7 10
C[[2]]
#> [1] 4 5

Created on 2019-07-12 by the reprex package (v0.2.1)

1 Like

thank you so much , but i want it to be vector of pairs like this
C={ (1, 2), (1, 3), (4, 5), (4, 6), (7, 9), (7, 10)}

Hi

I think what @FJCC gave you is exactly a vector with pairs of vectors. The only difference to your output is that it's not sorted by the first (key?) value. What are you going to do with the vector once it's created? Your question is too vague to give you the answer you need it seems ...

Hi
I'm using it as .dat file for AMPL code and the file should be exactly as i mentioned
C={ (1, 2), (1, 3), (4, 5), (4, 6), (7, 9), (7, 10)}
that's my problem , I know that what @FJCC are pairs vectors but the problem that it should be exactly like that to be understandable for AMPL , and it should be in one vector that's my problem.
i have this script which can be used to append pairs for a specific vectors it's working with for loop but it's not working for lapply
a[[1]]<-append(a[[1]],set(pair(B[1,1],B[1,2])))
Thanks

Something like this?

library(sets)

B <- matrix(data = c(1, 4, 1, 4, 7, 7,
                     2, 5, 3, 6, 9, 10),
            ncol = 2)

a <- as.set(apply(X = B,
                  MARGIN = 1,
                  FUN = function(t) pair(t[1], t[2])))
a
#> {(1, 2), (1, 3), (4, 5), (4, 6), (7, 9), (7, 10)}

Created on 2019-07-13 by the reprex package (v0.3.0)

I've never used sets before, so I don't know whether it'll be faster or not.

2 Likes

this is exactly what i want , thanks a lot :slight_smile:

Whenever you want to simplify a for look, look for one of the "apply" family of functions: lapply, sapply, mapply, apply, ...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.