Skip to content
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

foverlaps throws cryptic error when provided factors #2645

Closed
sritchie73 opened this issue Feb 26, 2018 · 0 comments
Closed

foverlaps throws cryptic error when provided factors #2645

sritchie73 opened this issue Feb 26, 2018 · 0 comments
Assignees
Labels

Comments

@sritchie73
Copy link
Contributor

@sritchie73 sritchie73 commented Feb 26, 2018

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
Projects
None yet
2 participants
You can’t perform that action at this time.