重塑包装遮盖,防止熔化命名柱

时间:2013-06-03 12:23:30

标签: r plyr reshape reshape2 melt

我有一个需要reshapereshape2库的脚本。我知道这是不好的做法,但我认为 plyr(或我正在使用的其他库) Vennerable正在加载reshape并且我亲自使用{{1}在许多地方。

问题是reshape2 reshape2的屏蔽导致了reshape函数的问题

melt

我以为我可以使用# 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 melt专门致电reshape2,但我仍然遇到同样的问题。

有一个简单的方法吗?如果不是,我将不得不在每次熔化后立即手动重新标记列名。

2 个答案:

答案 0 :(得分:7)

使用reshape2:::melt.data.frame(...)

melt实际上是一种方法:

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

因此,对于数据框,R将搜索melt.data.frame中的reshape

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

正如我所指出的,最好的解决方案可能只是升级所有内容。 plyr用于加载reshape,但它已经不存在了。(编辑:我在考虑ggplot2。)

答案 1 :(得分:0)

考虑卸载重塑包并在必要时重新加载

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