Convert pair(1L,2L) to pair(1,2)

rstudio

#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)
i'm using sets package


Removing duplicate in list of pairs
#2

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff.

Right now the best way to install reprex is:

# install.packages("devtools")
devtools::install_github("tidyverse/reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.


#3

I apologize if this is obvious, but since I can’t quite tell from your question what your expectation is here... are you aware that the L suffix is R’s syntax for representing integer constants? (Without an L, even if a numeric constant is entered and displayed without decimal places, it is still treated by R as a decimal constant).

So if you were trying to create pairs of integers, that’s exactly what you’ve got. If you were hoping for something else, then I think you need to provide more information about exactly what you’re trying to do.


#4

thank you for answer but the problem that i compare these pairs (1L,1L),(1L,2L)..... with vector contain other pairs :{(1, 2), (4, 7), (6, 7)} ,so when i compare pair (1L,2L) with (1,2) give me false that mean they are not the same ???!!!!


#5

Note that in the documentation file for :, (see ?':'), the return value is described

For numeric arguments, a numeric vector. This will be of type integer if from is integer-valued and the result is representable in the R integer type, otherwise of type "double" (aka mode "numeric").

Since both 1 and 4 can be represented as integers, the return is a vector of class integer. In some circumstances, R will print integers with an L to denote that it has the integer class. This seems to be what you are experiencing. If you wish to force the results to be non-integer class, perhaps the following will work:

library(sets)
for (i in 1:4){ 
  for (j in 1:4){
    print(pair(as.double(i),
               as.double(j)))
  }
}

#6

nutterb's solution sounds good to me.

In case it helps, here's a short example to illustrate in what circumstance c(1L, 2L) is considered identical to c(1, 2)

a <- c(1L, 2L)
b <- c(1, 2)
identical(a, b) 
#> [1] FALSE
identical(as.numeric(a), b)
#> [1] TRUE
identical(a, as.integer(b))
#> [1] TRUE

#7

Please follow @mara’s advice and provide a reproducible example of your entire problem — it’s not very efficient for you or your helpers to go back and forth when your helpers don’t have the full picture.

That said, you might want to read up on the details of comparison in R, including use of the identical and all.equal functions.

library(sets)

pair(1L, 2L) == pair(1, 2)
#> (TRUE, TRUE)

identical(pair(1L, 2L), pair(1, 2))
#> [1] FALSE

all.equal(pair(1L, 2L), pair(1, 2))
#> [1] TRUE

str(pair(1L, 2L))
#> List of 2
#>  $ : int 1
#>  $ : int 2
#>  - attr(*, "class")= chr "tuple"

str(pair(1, 2))
#> List of 2
#>  $ : num 1
#>  $ : num 2
#> c - attr(*, "class")= chr "tuple"

#8

hello
i have a list of pairs for example {(1,2),(2,1),(4,5),(5,4)}, what i'm trying to do to remove the duplicate by deleting the pairs (2,1),(5,4) so the list will be {(1,2),(4,5)} , i have this code

v<- lapply(v, function(s) {s[[NULL]] <- NULL; s})
v<-lapply(v,function(y){x1 <- unlist(y)
l1 <-unique(lapply(split(as.vector(x1),as.vector(gl(length(x1), 2,length(x1)))), sort))
do.call(set,lapply(l1, function(y) pair(y[1], y[2])))

now this code working very will but sometimes it gave me this error

Error in split.default(as.vector(x1), as.vector(gl(length(x1), 2, length(x1)))) : first argument must be a vector for example if i have this list of pairs

i'm using Rstudio with sets and igraph packages
to start you can use this simple example

v<-vector(mode = "list",length = 2)
v[[1]]<-set(pair(1,2))
v[[1]]<-append(x[[1]],set(pair(2,1)))
v[[2]]<-set(pair(4,5))
v[[2]]<-append(x[[2]],set(pair(5,4)))

#9

Hi @ahmad_anaqreh,

If you format your post like so:

```{r}
# Insert code here
```

It's much easier to read the code. Also, when you make your reproducible example, as @mara mentions, please include everything needed including which packages you use, i.e. the code should be able run top-to-bottom. It a lot easier to help you if we have all the information :slightly_smiling_face:


#10
library(sets)
library(igraph)
M<-erdos.renyi.game(8,0.6)
plot(M)
#vector of sets
v<-vector(mode = "list",length = 8)
a<-rep(0,2)
d<-rep(0,8)

for(k in 1: length(M))
{
  #find shortest paths for each node in the graph 
  c<-all_shortest_paths(M,k)
  #for loop on all shortest paths for each node  
  for(i in 1:length(c$res))
  {
    z<-c$res[[i[1]]]
    l<-length(z)
    #make sure the length of the shortest path more than two 
    if (l > 2 )
    {
      a[1]<-c$res[[i[1]]][1]
      e<-a[1]
      a[2]<-c$res[[i[1]]][l]
      f<-a[2]
    
      for(j in 2:l-1)
      {
        if (c$res[[i[1]]][j] != k)
        {
        d[j]<-c$res[[i[1]]][j]
        v[[d[j]]]<-append(v[[d[j]]],set(pair(e,f)))
      } 
      }
    }
    
  }
  
}
#delet NULL values if it's exist 
v<- lapply(v, function(s) {s[[NULL]] <- NULL; s})
#delet the duplicates if it's exist for example : (1,2),(2,1)  
v<-lapply(v,function(y){x1 <- unlist(y)
l1 <-unique(lapply(split(as.vector(x1),as.vector(gl(length(x1), 2,length(x1)))), sort))
do.call(set,lapply(l1, function(y) pair(y[1], y[2])))
})

print(v)

right now i put the whole code i'm using , if you try to run it it will show you the problem that i mentioned before
thanks in advance


#11

Will this solve your challenge?

set = list(c(1,2), c(2,1), c(4,5), c(5,4))
set
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 2 1
# 
# [[3]]
# [1] 4 5
# 
# [[4]]
# [1] 5 4

set = lapply(set, sort)
set
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 1 2
# 
# [[3]]
# [1] 4 5
# 
# [[4]]
# [1] 4 5

set = unique(set)
set
# [[1]]
# [1] 1 2
# 
# [[2]]
# [1] 4 5

#12

hello Leon
what you are doing that you supose only one pair in each vector but as i showed in the example it can be 20 pairs ,for example
[[1]]
{ (1, 5), (1, 6), (1, 8), (2, 6), (2, 8), (3, 6), (5, 1), (6, 1), (6, 2), (6, 3), (6, 8),
(8, 1), (8, 2), (8, 6)}
your way will not work with this kind of vector


#13

Perhaps I do not fully appreciate your challenge, but I think the code I wrote does what you ask here?


#14

in my case the pairs (1,5) ,(5,1) are not needed so i want to delete the (5,1) and keep (1,5) the same for all other pairs,one more thing my friend in your example you are working on list of integers but in my case it's list of pairs


#15

I see @ahmad_anaqreh, in that case, try running this function then:

library("sets")
rm_duplicates = function(x){
  x = lapply(x, as.numeric)
  x = lapply(x, sort)
  x = unique(x)
  x = lapply(x, as.tuple)
  return(x)
}
set = list(pair(1, 5), pair(1, 6), pair(1, 8), pair(2, 6), pair(2, 8),
           pair(3, 6), pair(5, 1), pair(6, 1), pair(6, 2), pair(6, 3),
           pair(6, 8), pair(8, 1), pair(8, 2), pair(8, 6))
print(set)
print(rm_duplicates(x = set))

Hope it helps? :slightly_smiling_face:


#16

Did this work for you @ahmad_anaqreh? If so, please remember to mark the solution :slightly_smiling_face: