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


#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"