by = .EACHI and summing over the empty set returning NA
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
Research direction
Reproduce the supplied R data.table example, including the .EACHI query with the empty pseudacorus group and the extra length and is.na checks. Compare the results with base R's sum(numeric(0)) behavior and determine the intended handling of empty groups. Done means the behavior is clarified or corrected and covered by an appropriate regression test.
Written by the indexing model from the issue text.
Description
Is the following the correct behaviour?
irisDT <- data.table(iris)
our_species <- c("setosa", "versicolor", "virginica", "pseudacorus")
irisDT[, Species := factor(Species, levels = our_species)]
setkey(irisDT, Species)
irisDT[J(our_species), .(.N, tsl = sum(Sepal.Length)), by = .EACHI]
# Species N tsl
# 1: setosa 50 250.3
# 2: versicolor 50 296.8
# 3: virginica 50 329.4
# 4: pseudacorus 0 NA
# Expected to get 0 not NA
sum(numeric(0)) # [1] 0; the documentation says "the sum of an empty set is zero, by definition"
# Current solution
irisDT[J(our_species), .(.N, tsl = sum(Sepal.Length)), by = .EACHI
][, tsl := fifelse(N>0L, tsl, 0)][]
## EDIT (cleaner solution)
irisDT[J(our_species), .(.N, tsl = sum(Sepal.Length, na.rm = TRUE)), by = .EACHI]
## EDIT ENDS
# Extra surprise
irisDT[1][J(our_species), .(.N, tsl = length(Sepal.Length)), by = .EACHI]
# It seems that we are dealing with a NA_real_ of length 1
irisDT[1][J(our_species), .(.N, tsl = is.na(Sepal.Length)), by = .EACHI]
Sorry if this has been discussed/documented elsewhere.
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·