6

I have a script which requires both reshape and reshape2 libraries. I know this is poor practise, but I think plyr (or another library I am using) Vennerable is loading reshape and I have personally used reshape2 in a lot of places.

The problem is that the masking of reshape2 by reshape is causing problems for the melt function

# Example data frame
df <- data.frame(id=c(1:5), a=c(rnorm(5)), b=c(rnorm(5)))

# With just reshape2, variable and value columns are labelled correctly
library(reshape2)
melt(df, measure.vars=c("a", "b"), variable.name="type", value.name="distance")
   id type   distance
1   1    a -2.0233666
2   2    a  0.4625188
3   3    a -2.8688127
4   4    a  0.8151644
5   5    a -0.4574464
6   1    b  1.3197784
7   2    b  1.6213146
8   3    b  1.3508913
9   4    b -1.6483839
10  5    b -1.1342157

# But my script also has reshape loaded
library(reshape)
Loading required package: plyr

Attaching package: ‘reshape’

The following object(s) are masked from ‘package:plyr’:

    rename, round_any

The following object(s) are masked from ‘package:reshape2’:

    colsplit, melt, recast

# When calling melt in this environment, variable and value columns stick to 
# their default names
melt(df, measure.vars=c("a", "b"), variable.name="type", value.name="distance")
   id variable      value
1   1        a -2.0233666
2   2        a  0.4625188
3   3        a -2.8688127
4   4        a  0.8151644
5   5        a -0.4574464
6   1        b  1.3197784
7   2        b  1.6213146
8   3        b  1.3508913
9   4        b -1.6483839
10  5        b -1.1342157

I thought I could specifically call melt from reshape2 using reshape2::melt but I still get the same problem.

Is there an easy way around this? If not I will have to manually relabel the column names straight after each melt call.

MattLBeck
  • 5,701
  • 7
  • 40
  • 56
  • 1
    I don't think the newest version of `plyr` uses `reshape` anymore. Is that the only reason you were importing `reshape`? – Peyton Jun 03 '13 at 12:28
  • I don't import it personally, but I think another library is and I can't work out which. I suspected it was plyr because of the ordering of the ouput. – MattLBeck Jun 03 '13 at 12:31
  • 1
    Check each of the packages like this: `installed.packages()[installed.packages()[,1]=="ggplot2",]` – Roland Jun 03 '13 at 12:41
  • 2
    Perhaps `update.packages()` could do wonders. – Henrik Jun 03 '13 at 12:47
  • @Roland thanks for the tip! I found that the culprit was `Vennerable`. I will do a `update.packages` anyway but I don't think `Vennerable` has been updated since my current 2.2 version. – MattLBeck Jun 03 '13 at 12:59

2 Answers2

7

Use reshape2:::melt.data.frame(...).

melt is actually a method:

> reshape2::melt
function (data, ..., na.rm = FALSE, value.name = "value") 
{
    UseMethod("melt", data)
}
<environment: namespace:reshape2>

So, in the case of a data frame, R will search for melt.data.frame, which is in reshape:

> melt.data.frame
function (data, id.vars, measure.vars, variable_name = "variable", 
    na.rm = !preserve.na, preserve.na = TRUE, ...) 
{
    ...
}
<environment: namespace:reshape>

As I indicated though, the best solution might just be to upgrade everything. It is true that plyr used to load reshape, but it doesn't anymore. (Edit: I was thinking ggplot2.)

Peyton
  • 7,266
  • 2
  • 29
  • 29
  • Ah, brilliant. I was also trying `reshape2::melt.data.frame(...)` but wasn't aware I needed three colons (it was complaining that `melt.data.frame` wasn't exported otherwise). Also, cheers for the upgrading tip. – MattLBeck Jun 03 '13 at 12:39
  • 1
    Yes, the triple colon operator will grab internal (i.e., not exported) values. – Peyton Jun 03 '13 at 12:40
  • be sure to specify the appropriate form for your dataset, in this case melt.data.frame, but other forms include: melt.array, melt.list, melt.matrix, melt.table and melt.default (for vectors) – woodvi Feb 19 '16 at 17:50
0

Consider unloading the reshape package and reloading it again when necessary

detach("package:reshape", unload=TRUE)
user2387584
  • 323
  • 4
  • 7