Skip to content

foverlaps throws cryptic error when provided factors #2645

Closed

Description

@sritchie73

The foverlaps() function can only work with interval columns that are numeric (integer, double, integer64).

When trying foverlaps() with character interval columns you get an error message telling you this:

# Provided non numeric data, foverlaps throws a sensible error:
X <- data.table(id=1, start="a", end="d")
Y <- data.table(id=2, start="b", end="e")
setkey(X, start, end)
setkey(Y, start, end)
foverlaps(X , Y)
# Error in foverlaps(X, Y) : 
#   The last two columns in by.x should correspond to the 'start' and 'end' intervals 
#    in data.table 'x' and must be integer/numeric type.

When trying with a factor, you get a cryptic error message:

# The same, but coded as a factor
lf <- factor(letters)
X <- data.table(id=1, start=lf[1], end=lf[4])
Y <- data.table(id=2, start=lf[2], end=lf[5])
setkey(X, start, end)
setkey(Y, start, end)
foverlaps(X, Y)
# Error in if (any(x[[xintervals[2L]]] - x[[xintervals[1L]]] < 0L)) stop("All entries 
# in column ",  : 
#   missing value where TRUE/FALSE needed
# In addition: Warning message:
# In Ops.factor(x[[xintervals[2L]]], x[[xintervals[1L]]]) :
#   ‘-’ not meaningful for factors

The same is true if (for whatever reason) you have numeric interval columns coded as factors:

# Numeric factors also have this problem:
nf <- factor(sort(rnorm(10)))
X <- data.table(id=1, start=nf[1], end=nf[4])
Y <- data.table(id=2, start=nf[2], end=nf[5])
setkey(X, start, end)
setkey(Y, start, end)
foverlaps(X, Y)
# Error in if (any(x[[xintervals[2L]]] - x[[xintervals[1L]]] < 0L)) stop("All entries 
# in column ",  : 
#   missing value where TRUE/FALSE needed
# In addition: Warning message:
# In Ops.factor(x[[xintervals[2L]]], x[[xintervals[1L]]]) :
#   ‘-’ not meaningful for factors

In these cases, the foverlaps() function should throw the first error message stating that it cannot work with factors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions