问题描述
我有一个同时需要reshape
和reshape2
库的脚本.我知道这是很差的做法,但是我认为 Vennerable
正在加载reshape
,我个人在很多地方都使用过reshape2
.
I have a script which requires both reshape
and reshape2
libraries. I know this is poor practise, but I think Vennerable
is loading reshape
and I have personally used reshape2
in a lot of places.
问题是reshape
对reshape2
的屏蔽导致melt
函数出现问题
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
我以为我可以使用reshape2::melt
从reshape2
专门调用melt
,但是我仍然遇到同样的问题.
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.
推荐答案
使用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
中:
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>
正如我所指出的,最好的解决方案可能只是升级所有内容. (我当时在考虑ggplot2.)
As I indicated though, the best solution might just be to upgrade everything. ( I was thinking ggplot2.)
这篇关于重塑包装掩膜的形状,以防止熔化物命名色谱柱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!