Can't make the option value.name work in reshape2::melt()

219 views
Skip to first unread message

Pierre Roudier

unread,
Jan 13, 2011, 7:50:05 PM1/13/11
to manip...@googlegroups.com
Dear list,

I am using reshape2 and the great melt() to reshape a matrix.
I would like to use the option value.name to give the value column of
the melted result a personalized name, but that does not work.

Here is a reproducible example:

require(reshape2)
b <- matrix(0, nrow=5, ncol=20)
colnames(b) <- 1:20
rownames(b) <- LETTERS[1:5]
names(melt(b, varnames=c('foo', 'bar'), value.name='baz'))

And this is my sessionInfo():

R version 2.12.1 Patched (2011-01-10 r53950)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] reshape2_1.1 doSNOW_1.0.3 snow_0.3-3 foreach_1.3.0
[5] codetools_0.2-7 iterators_1.0.3 ggplot2_0.8.9 proto_0.3-8
[9] reshape_0.8.3 plyr_1.4 pls_2.1-0 stringr_0.4
[13] rgdal_0.6-33 sp_0.9-76

loaded via a namespace (and not attached):
[1] digest_0.4.2 lattice_0.19-17 tcltk_2.12.1 tools_2.12.1

Cheers,

Pierre

--
Scientist
Landcare Research, New Zealand

Dennis Murphy

unread,
Jan 13, 2011, 9:11:15 PM1/13/11
to Pierre Roudier, manip...@googlegroups.com
Hi:

I have ggplot2 autoloaded on startup in this session, so reshape is in the search path. Loading reshape2, I get the following:

library(reshape2)
u <- reshape2:::melt.array(b, varnames=c('foo', 'bar'), value.name = 'baz')
> head(u, 3)
  foo bar baz
1   A   1   0
2   B   1   0
3   C   1   0

# This apparently uses the reshape package's version:
# (melt() gets the same result since b is an array)
> u <- melt.array(b, varnames=c('foo', 'bar'), value.name = 'baz')
> head(u, 3)
  foo bar value
1   A   1     0
2   B   1     0
3   C   1     0

I find this interesting, since reshape2 is higher in the search path:
> search()
 [1] ".GlobalEnv"        "package:reshape2"  "package:stats"   
 [4] "package:graphics"  "package:grDevices" "package:utils"   
 [7] "package:datasets"  "package:sos"       "package:brew"    
[10] "package:lattice"   "package:ggplot2"   "package:proto"   
[13] "package:grid"      "package:reshape"   "package:plyr"    
[16] "package:methods"   "Autoloads"         "package:base"

> sessionInfo()
R version 2.12.1 Patched (2010-12-18 r53869)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                         
[5] LC_TIME=English_United States.1252   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  grid      methods 
[8] base    

other attached packages:
[1] reshape2_1.1    sos_1.3-0       brew_1.0-4      lattice_0.19-17
[5] ggplot2_0.8.9   proto_0.3-8     reshape_0.8.3   plyr_1.4      

loaded via a namespace (and not attached):
[1] stringr_0.4  tools_2.12.1

# -----------------
I decided to try this in a fresh R session in a directory where I have no packages autoloaded, and got the following:

> library(reshape2)

> b <- matrix(0, nrow=5, ncol=20)
> colnames(b) <- 1:20
> rownames(b) <- LETTERS[1:5]
> names(melt(b, varnames=c('foo', 'bar'), value.name='baz'))
[1] "foo" "bar" "baz"

> search()
 [1] ".GlobalEnv"        "package:reshape2"  "package:stats"   
 [4] "package:graphics"  "package:grDevices" "package:utils"   
 [7] "package:datasets"  "package:methods"   "Autoloads"       
[10] "package:base"
#--------------------------

Evidently, having both reshape and reshape2 in the search path causes the problem. If ggplot2 is loaded, one can't detach reshape:

> detach(package:reshape)
Error: package ‘reshape’ is required by ‘ggplot2’ so will not be detached

so it appears that you'll have to specify the reshape2 version of melt.array() as shown above to get the desired behavior for the time being when ggplot2 is loaded.

HTH,
Dennis




--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To post to this group, send email to manip...@googlegroups.com.
To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.


Pierre Roudier

unread,
Jan 13, 2011, 11:25:34 PM1/13/11
to Dennis Murphy, manip...@googlegroups.com
Thanks Dennis, specifying the package using ::: works.

Pierre

2011/1/14 Dennis Murphy <djm...@gmail.com>:

Hadley Wickham

unread,
Jan 14, 2011, 9:01:59 AM1/14/11
to Pierre Roudier, Dennis Murphy, manip...@googlegroups.com
This will be fixed in the next version of ggplot2, which not only will
use reshape2, but will also use namespaces properly so this type of
problem doesn't occur.

Hadley

On Thu, Jan 13, 2011 at 10:25 PM, Pierre Roudier

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages