Wet Feet
Wet Feet

Reputation: 4555

R: Help in melting a data frame "with differing rows"

I have a data frame that I cannot reshape2 :: melt. Can anyone help me see why?

> dput(x2)
structure(list(`26492` = c(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, 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, 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, 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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, 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

Upvotes: 3

Views: 1595

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193527

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

Upvotes: 5

Related Questions