Change masking of packages that prevent melt from column names

I have a script that requires reshape and reshape2 . I know this is bad practice, but I think that plyr (or another library that I use) is Vennerable loaded by reshape , and I personally used reshape2 in many places.

The problem is that masking reshape2 on reshape causes 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 call melt from reshape2 using reshape2::melt , but I am getting the same problem anyway.

Is there an easy way around this? If not, I will have to manually re-mark the column names immediately after each melt call.

+6
source share
2 answers

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 look 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 said, the best solution would be to simply upgrade everything. It is true that plyr used to load reshape , but this is no longer the case. (Edit: I thought ggplot2.)

+7
source

Consider unloading the change package and reloading it if necessary

 detach("package:reshape", unload=TRUE) 
0
source

Source: https://habr.com/ru/post/946406/


All Articles