Skip to content

add factor and ordered for integer64#255

Merged
hcirellu merged 19 commits intomainfrom
factor
Feb 23, 2026
Merged

add factor and ordered for integer64#255
hcirellu merged 19 commits intomainfrom
factor

Conversation

@hcirellu
Copy link
Copy Markdown
Collaborator

@hcirellu hcirellu commented Feb 2, 2026

The base coercion from integer64 to factor does not work properly regarding the sorting of the levels.

Actual behavior:

factor(as.integer64(c(132724613L, -2143220989L, -1L, NA, 1L)))
# [1] 132724613   -2143220989 -1          <NA>        1          
# Levels: 1 132724613 -2143220989 -1
factor(c(132724613L, -2143220989L, -1L, NA, 1L))
# [1] 132724613   -2143220989 -1          <NA>        1          
# Levels: -2143220989 -1 1 132724613

Expected behavior for integer64:

factor(as.integer64(c(132724613L, -2143220989L, -1L, NA, 1L)))
# [1] 132724613   -2143220989 -1          <NA>        1          
# Levels: -2143220989 -1 1 132724613

In order to have table work consistent with base R, this correct factor conversion is necessary.

In addition, the explicit conversion should also be faster for larger vectors.

x <- sample(as.integer64(c(132724613L, -2143220989L, -1L, NA, 1L)), rep=T, size = 1000000)
microbenchmark::microbenchmark(factor(x), base::factor(x))
# Unit: milliseconds
#             expr     min       lq     mean   median       uq      max neval
#        factor(x) 25.5235 28.22665 50.36219 35.63280 41.25975 238.0824   100
#  base::factor(x) 70.0936 78.46005 88.04246 82.59635 89.18355 291.1204   100

could not find function "expect_failure" in test-ancient
@hcirellu hcirellu marked this pull request as ready for review February 2, 2026 08:25
hcirellu added a commit that referenced this pull request Feb 2, 2026
@MichaelChirico

This comment was marked as outdated.

@hcirellu

This comment was marked as outdated.

Comment thread R/integer64.R Outdated
Comment thread R/integer64.R
Comment thread R/integer64.R Outdated
Comment thread tests/testthat/test-integer64.R
Comment thread R/highlevel64.R
Comment thread R/highlevel64.R
Copy link
Copy Markdown
Collaborator

@MichaelChirico MichaelChirico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically ready to go, just a few dangling comments to make sure the edits to the benchmarking code are WAI.

Feel free to merge at your discretion.

@hcirellu hcirellu merged commit ffb1ce0 into main Feb 23, 2026
10 checks passed
hcirellu added a commit that referenced this pull request Feb 23, 2026
@MichaelChirico MichaelChirico deleted the factor branch February 24, 2026 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants