Skip to content

melt(, na.rm=T) doesn't handle NA-columns properly #1359

Closed

Description

@mplatzer

The code

dt <- data.table(id=1:3, x=NA_character_, y=c('a', NA_character_, 'c'))
melt(dt, id.var='id', na.rm=TRUE)

produces in data.table v1.9.4

   id variable value
1:  1        y     a
2:  3        y     c

but in data.table v.1.9.6 incorrectly returns

   id variable value
1:  1       NA     a
2:  3       NA     c

It seems the bug only occurs when melt is called with na.rm=TRUE on a data.table with a column that purely consists of NA-values. Also note, that the bug does not occur, when the NA-column is the last column of the data.table.

Activity

  1. oseiskar commented on Sep 25, 2015

    @oseiskar
    Contributor

    There is also something seriously wrong with the resulting NA values:

    library(data.table)
    dt <- melt(data.table(x='a',y=NA_character_,z='c'), id.var=c('x'), na.rm=T)
    
    melt_na <- dt$variable
    regular_na <- as.factor(c(NA_character_,'z'))[1]

    Both melt_na and regular_na show as

    [1] <NA>
    Levels: z
    

    but write.table(melt_na) crashes with the error message

    Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,  : 
      index out of range
    
  2. added this to the v1.9.8 milestone on Sep 25, 2015
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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions