-1

my problem is i'm trying to get pairs by this code:
for (i in 1:4) { for (j in 1:4) { print(pair(i,j))
} }

my expected result to be (1,1),(1,2).... not (1L,1L),(1L,2L)

1 Answers1

0

-- answer to question before edit --

I'm assuming you mean pairlist and not pair.

If so you can do the following:

v <- list(pairlist(1,2),pairlist(3,4))

if (any(sapply(v,identical,pairlist(1,2))))
  print(1)
# [1] 1

if (any(sapply(v,identical,pairlist(2,2))))
  print(1)
# *nothing happens*
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • thank you so much , it's working , i have another question please ,now instead of using pair(1,2) i wanna use variables for example:pair(i,j) ,the problem that the R display it like (1L, 2L),do you have any idea how i can keep it with the value of the variable which is integer ??? – ahmad alanaqreh May 30 '18 at 11:11
  • I don't understand the issue, you want to be flexible regarding `integer` to `double` comparison ? – moodymudskipper May 30 '18 at 11:16
  • Also I'm confused that you keep on saying `pair` and not `pairlist`, what function are you actually using ? – moodymudskipper May 30 '18 at 11:17
  • i am using vector called v the mode is "list" ,inside each element i have pairs "v<-vector(mode = "list",length = 10)",now what i'm doing that i use for loop with two variables i and j ,so when i am comparing depends on the value pair(i,j) it doesn't work , if i print it out the value of pair(i,j) the result like this (1L,2L) where the value of i is 1 the value of j is 2 – ahmad alanaqreh May 30 '18 at 11:32
  • `L` just means it's an integer, you can write `if (any(sapply(v,identical,pairlist(1L,2L))))` – moodymudskipper May 30 '18 at 11:36
  • To assist you further I'd really need a reproducible example – moodymudskipper May 30 '18 at 11:37
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – moodymudskipper May 30 '18 at 11:37
  • for (i in 1:10) { for (j in 1:11) { if(any(sapply(v[[i]],identical,pair(i,j)))){Y[i,j]=1} } } – ahmad alanaqreh May 30 '18 at 11:59
  • not like this :). Poste a `dput` of a sample of your input data, and type exactly what output you would like to get, and edit your question, not the comments, the post I linked should detail all this. – moodymudskipper May 30 '18 at 12:01