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, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("26492", "28728"), class = c("data.table", 
"data.frame"), row.names = c(NA, -286L), .internal.selfref = <pointer: 0x0000000000330788>)

> melt(x2)
Using  as id variables
Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 0, 572

I also created this data frame with a NA value inside and melt works, so I'm not sure where the problem is.

> df1 <- data.frame(x = rnorm(2), y = rnorm(2))
> df1[1,1]<-NA
> df1
           x          y
1         NA -1.0138754
2 -0.7848228  0.3117672
> melt(df1)
Using  as id variables
  variable      value
1        x         NA
2        x -0.7848228
3        y -1.0138754
4        y  0.3117672
4
  • This is probably unrelated to your question but you should avoid naming columns as numbers. Add a character as a suffix or something, if you really need to keep that number as part of the column name. stackoverflow.com/questions/3423085/… Commented Nov 8, 2013 at 3:37
  • Hi, even after renaming the columns to "a" and "b", the error remains. But your tip is noted. :) Commented Nov 8, 2013 at 3:39
  • 1
    @JakeBurkhead, that doesn't work for me. However, when I convert x2 to a data.frame, melt works for me. Commented Nov 8, 2013 at 3:52
  • I can't reproduce your error with the names changed to characters and the .internetal.selfref argument ignored. Commented Nov 8, 2013 at 4:08

1 Answer 1

5

data.frames and data.tables, while related, are different beasts and should be treated differently. This is why in data.table Version 1.8.11, a new melt method was provided.

To summarize what I found, using melt.data.frame will give you this error on a data.table but not on a data.frame, so you should either use as.data.frame(YOUR_DATA_TABLE) or (recommended) update your version of data.table.

Example:

library(reshape2)
library(data.table)

packageVersion("data.table")
# [1] ‘1.8.11’

## WORKS WITH WARNING THAT YOU DID NOT 
##  SUPPLY ID AND MEASURE VARIABLES
data.table:::melt.data.table(x2)
#      variable value
#   1:    26492     0
#   2:    26492     0
#   3:    26492     0
#   4:    26492     0
#   5:    26492     0
# ---               
# 568:    28728     0
# 569:    28728     0
# 570:    28728     0
# 571:    28728     0
# 572:    28728     0
# Warning message:
# In data.table:::melt.data.table(x2) :
#   To be consistent with reshape2's melt, id.vars and measure.vars 
#   are internally guessed when both are 'NULL'. All non-numeric/integer/
#   logical type columns are conisdered id.vars, which in this case are 
#   columns ''. Consider providing at least one of 'id' or 'measure' vars 
#   in future.

So, with data.table 1.8.11 and up, which introduce this new melt method, things work. Where's the error you speak of?

## HERE'S YOUR ERROR
reshape2:::melt.data.frame(x2)
# Using  as id variables
# Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
#   arguments imply differing number of rows: 0, 572

## HERE'S A WORKAROUND
head(reshape2:::melt.data.frame(as.data.frame(x2)))
Using  as id variables
#   variable value
# 1    26492     0
# 2    26492     0
# 3    26492     0
# 4    26492     0
# 5    26492     0
# 6    26492     0

This works with your small example too.

df1 <- data.frame(x = rnorm(2), y = rnorm(2))
df1[1,1]<-NA
DT <- data.table(df1)
reshape2:::melt.data.frame(DT)                ## ERROR
reshape2:::melt.data.frame(as.data.frame(DT)) ## NO ERROR
data.table:::melt.data.table(DT)              ## Warning. NO ERROR

Update

If there is an ID var specified, it appears that this error does not occur:

df1 <- data.frame(matrix(rnorm(6), ncol = 3))
df1[1, 2] <- NA
df1    

DT <- data.table(df1)
reshape2:::melt.data.frame(DT, id.vars="X1")    ## NO ERROR
#           X1 variable       value
# 1  1.3586796       X2          NA
# 2 -0.1027877       X2 -0.05380504
# 3  1.3586796       X3 -1.37705956
# 4 -0.1027877       X3 -0.41499456
data.table:::melt.data.table(DT, id.vars="X1")  ## NO ERROR
#            X1 variable       value
# 1:  1.3586796       X2          NA
# 2: -0.1027877       X2 -0.05380504
# 3:  1.3586796       X3 -1.37705956
# 4: -0.1027877       X3 -0.41499456
Sign up to request clarification or add additional context in comments.

2 Comments

I just downloaded data.table v1.8.11 and cannot find data.table:::melt.data.table. Am I misunderstanding?
Nevermind. I pulled the zip file off the data.table page that got compiled on Sept 3, before the new melt was added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.