gpt4 book ai didi

reshape 包掩码防止熔化命名列

转载 作者:行者123 更新时间:2023-12-04 14:46:33 31 4
gpt4 key购买 nike

我有一个脚本同时需要 reshapereshape2图书馆。我知道这是不好的做法,但我认为 plyr (或我正在使用的另一个库)Vennerable正在加载 reshape我个人使用过 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使用 reshape2::melt但我仍然遇到同样的问题。

有没有简单的方法来解决这个问题?如果不是,我将不得不在每次熔断调用后直接手动重新标记列名称。

最佳答案

使用 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。)

关于 reshape 包掩码防止熔化命名列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16896632/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com