-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
int64 summation fails #1545
Comments
The issue you're experiencing is due to the use of the base R sum function with integer64 objects from the bit64 package. The base R sum function does not handle integer64 objects correctly, which is causing the incorrect result. You should use the bit64::sum.integer64 function instead, which is designed to correctly handle integer64 objects. Here's the corrected code: tibble::tribble(
~ group, ~ `1`, ~ `2`, ~ `3`,
"A", bit64::as.integer64(4), bit64::as.integer64(3), bit64::as.integer64(4)
) |>
dplyr::group_by(group) |>
tidyr::nest() |> # creates 'data' column
dplyr::mutate(
x = purrr::map(data, function(x) {list("sum"=bit64::sum.integer64(unlist(x)))})
) |>
tidyr::unnest_wider(c(data, x))
#> # A tibble: 1 × 5
#> # Groups: group [1]
#> group `1` `2` `3` sum
#> <chr> <int64> <int64> <int64> <int64>
#> 1 A 4 3 4 11 This should resolve the issue and give you the correct sum for your integer64 values. |
Technically it's the unlist causing the problem, not sum. Fortunately, there's an easier way to tackle this with dplyr: use library(dplyr, warn.conflicts = FALSE)
df <- tribble(
~ group, ~ `1`, ~ `2`, ~ `3`,
"A", bit64::as.integer64(4), bit64::as.integer64(3), bit64::as.integer64(4)
)
df |> mutate(sum = sum(c_across("1":"3")))
#> # A tibble: 1 × 5
#> group `1` `2` `3` sum
#> <chr> <int64> <int64> <int64> <int64>
#> 1 A 4 3 4 11 Created on 2024-03-14 with reprex v2.1.0 I had actually forgotten how to do this, and found what I needed in https://dplyr.tidyverse.org/articles/rowwise.html#per-row-summary-statistics. |
Problem: 4 + 3 + 4 = 5.43-e323
![Screenshot 2024-03-11 at 4 13 10 PM](https://private-user-images.githubusercontent.com/68299186/311854004-b7b25ddd-2333-4c25-b267-4324fbc61568.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkxNjI5NjQsIm5iZiI6MTczOTE2MjY2NCwicGF0aCI6Ii82ODI5OTE4Ni8zMTE4NTQwMDQtYjdiMjVkZGQtMjMzMy00YzI1LWIyNjctNDMyNGZiYzYxNTY4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEwVDA0NDQyNFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTZjZmVhNzVhMzA1ZDRiMWI0YjljMTM1YTQxMmU0MjM3Mzg5OGFmNDUxZjc5ZTYxMzMzNjY2Mjc3ZTkzYTQ4ODUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.zLY4csh17XzRtCfpJbmbROLrxm3HNWUcHfvRAqfGAIo)
Reprex:
The text was updated successfully, but these errors were encountered: