Skip to content

NA as a factor level malformed by rbindlist #3915

Description

@sindribaldur
data.table(V1 = factor(as.character(c(NA, 1:100, NA)), exclude = NULL))
# Error in as.character.factor(x) : malformed factor
data.table(V1 = factor(as.character(c(NA, 1:3, NA)), exclude = NULL))
#      V1
# 1: <NA>
# 2:    1
# 3:    2
# 4:    3
# 5: <NA>

Is this the expected behaviour?

Another example here: https://stackoverflow.com/q/58103098/4552295

I'm getting this result with data.table_1.12.2 and R version 3.6.1.

Activity

  1. jangorecki commented on Sep 26, 2019

    @jangorecki
    Member

    Same in devel. Thanks for reporting. Happens when printing DT having > 100 rows because of head&tail print format.

    root cause of the issue is rbindlist in this line

    toprint = rbindlist(list(head(x, topn), tail(x, topn)), use.names=FALSE) # no need to match names because head and tail of same x, and #3306
    producing malformed factor, so the issue is actually in rbindlist. fyi @mattdowle
    minimal example

    x = data.table(V1 = factor(as.character(c(NA, 1:3, NA)), exclude = NULL))
    rbindlist(list(x), use.names=FALSE)$V1
    #Error in as.character.factor(x) : malformed factor
  2. self-assigned this
    on Sep 26, 2019
  3. added this to the 1.12.4 milestone on Sep 26, 2019
  4. changed the title [-]NA as a factor level in a data.table column[/-] [+]NA as a factor level malformed by rbindlist[/+] on Sep 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions