From 2eb1f1182468d30777519689e2d3527d15ea7d78 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Wed, 6 Apr 2016 14:29:28 -0700 Subject: [PATCH] melt.data.frame drops 'dim' attribute for 'value' column (#77) --- NEWS.md | 3 +++ R/utils.r | 8 ++++++-- tests/testthat/test-melt.r | 9 +++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 20db119..81b11ec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # Version 1.4.1.9000 +* `melt.data.frame()` always drops the `dim` attribute on generated + value columns. (#77) + * `melt.data.frame()` throws when encountering objects of type `POSIXlt`, and requests a conversion to the (much saner) `POSIXct` type. diff --git a/R/utils.r b/R/utils.r index e26ecf3..3d0761a 100644 --- a/R/utils.r +++ b/R/utils.r @@ -34,14 +34,18 @@ normalize_melt_arguments <- function(data, measure.ind, factorsAsStrings) { ## If we are going to be coercing any factors to strings, we don't want to ## copy the attributes - any.factors <- any( sapply( measure.ind, function(i) { - is.factor( data[[i]] ) + any.factors <- any(sapply(measure.ind, function(i) { + is.factor(data[[i]]) })) if (factorsAsStrings && any.factors) { measure.attributes <- NULL } + # Drop dimensions + if ("dim" %in% names(measure.attributes)) + measure.attributes[["dim"]] <- NULL + list( measure.attributes = measure.attributes, factorsAsStrings = factorsAsStrings diff --git a/tests/testthat/test-melt.r b/tests/testthat/test-melt.r index c0a2342..628b858 100644 --- a/tests/testthat/test-melt.r +++ b/tests/testthat/test-melt.r @@ -226,3 +226,12 @@ test_that("melt.data.frame throws when encountering POSIXlt", { expect_error(melt(df, measure.vars = c("x", "y"))) }) + +test_that("melt.data.frame drops dimensions on melted values", { + + df <- data.frame(x = 1:5, y = letters[1:5], stringsAsFactors = FALSE) + attr(df$x, "dim") <- 5 + melted <- melt(df, id = "y") + expect_true(is.null(attr(melted$value, "dim"))) + +})