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

diff(factor) by group causes STRING_ELT error #3315

Closed
MichaelChirico opened this issue Jan 25, 2019 · 1 comment
Closed

diff(factor) by group causes STRING_ELT error #3315

MichaelChirico opened this issue Jan 25, 2019 · 1 comment
Labels
Milestone

Comments

@MichaelChirico
Copy link
Member

MichaelChirico commented Jan 25, 2019

Reproducing for me on latest dev. I guess diff(factor) is not meaningful and should be an error, but it's going through and causing an error later down the line.

set.seed(32940)
NN = 7e5
KK = 4e4
TT = 25

DT = data.table(
  id = sample(KK, NN, TRUE),
  tt = sample(TT, NN, TRUE),
  ff = factor(sample(3, NN, TRUE))
)
DT[ , diff(ff), by = id]

Error in rbindlist(l, use.names, fill, idcol) :
STRING_ELT() can only be applied to a 'character vector', not a 'NULL'

traceback() shows it's coming from print:

6: rbindlist(l, use.names, fill, idcol) at data.table.R#2673
5: data.table::.rbind.data.table(...)
4: rbind(deparse.level, ...)
3: rbind(head(x, topn), tail(x, topn)) at print.data.table.R#53
2: print.data.table(x)
1: function (x, ...) 
   UseMethod("print")(x)
@MichaelChirico MichaelChirico changed the title STRING_ELT error coming up in interactive use in devel diff(factor) by group causes STRING_ELT error Jan 25, 2019
@MichaelChirico
Copy link
Member Author

MichaelChirico commented Jan 25, 2019

Actually I think this is an R bug:

diff(factor(1:3))

Error in as.character.factor(x) : malformed factor

x = diff(factor(1:3))
dput(x)
# structure(c(1L, 1L), class = "factor")

Warning message:
In str.default(val) : 'object' does not have valid levels()

(i.e. it's only an error to try and print() this broken object)

Whereas

x = factor(1:3)
x[3] - x[1]
# [1] NA

Warning message:
In Ops.factor(x[3], x[1]) : ‘-’ not meaningful for factors

Will try and file a fix with R-devel...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants