by = .EACHI and summing over the empty set returning NA

Open
#4,582 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

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

consistency joins

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.