Numeric variables with attributes cause melt.data.frame to fail #36

Closed
izahn opened this Issue Sep 6, 2013 · 3 comments

4 participants

@izahn
library(reshape2)

structure(list(ID = structure(1:10, 
                              .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), 
                              class = "factor"), 
               AGE = c(68L, 63L, 55L, 64L, 60L, 78L, 60L, 62L, 60L, 75L), 
               BMI = c(25L, 27L, 27L, 28L, 32L, NA, 36L, 27L, 31L, 25L), 
               EventDays = c(722L, 738L, 707L, 751L, 735L, 728L, 731L, 717L, 728L, 735L), 
               InterventionDays = c(NA, NA, 575, NA, NA, NA, 490, 643, NA, NA)), 
          .Names = c("ID", "AGE", "BMI", "EventDays", "InterventionDays"), 
          row.names = c(NA, -10L), 
          class = "data.frame")

melt(D, c("ID", "AGE", "BMI")) ## works

attr(D$ID, "label") <- "ID number"  ## add attribute to factor
melt(D, c("ID", "AGE", "BMI")) ## works

attr(D$AGE, "label") <- "Age" ## add attribute to numeric variable
melt(D, c("ID", "AGE", "BMI")) ## does not work

# Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
#   arguments imply differing number of rows: 10, 20
@kevinushey

I guess this is because the output data.frame depends on recycling of the id variables; R refuses to recycle if we have an attribute on a numeric variable.

Example:

x <- 1:5
y <- 1:10
data.frame(x, y) ## okay
attr(x, "foo") <- "bar"
data.frame(x, y) ## error

This is with R 3.0.1.

@hadley
Owner

Works now since the vectorisation now happens in C++

@hadley hadley closed this Mar 10, 2014
@nutterb

Thanks. It's greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment