【问题标题】:What code does a task like the reshape2 package in a base reshape function?像 reshape2 包这样的任务在基本 reshape 函数中执行什么代码?
【发布时间】:2019-08-24 13:37:47
【问题描述】:

我正在学习 reshape 函数(base R),它的工作原理类似于 hadley wickham 的 reshape2 包。 我使用reshape2 包编写了下面的代码。

melt(iris, id.vars = 'Species')

结果: 列名是 Species 、变量、值,结果的整数是 600

我写了这段代码

reshape(iris, idvar = 'Species', direction = 'long')

但是显示错误信息

重塑错误(iris, idvar = "Species", direction = "long") : "no “reshapeWide”属性,必须指定“可变”

如何查看在reshape2 包中执行melt 函数的相同结果?

【问题讨论】:

  • 嗨,Jh J,仅供参考,这些软件包不在基础中; reshapereshape2 包均由 Hadley 提供。并且这两个包都被tidyr 库所取代,其中包含gather()spread()。见tidyr.tidyverse.org
  • reshape 函数在基础 R 中。find("reshape")
  • @G.Grothendieck 你是对的,我混淆了基本函数stats::reshape()reshape::melt() 包。

标签: r reshape2


【解决方案1】:

至少指定varying,最好是所有显示的:

nm <- names(iris)[-5]
long <- reshape(iris, dir = "long",
  varying = list(nm), times = nm, timevar = "Attribute", v.names = "value")

long 的前几行是:

> head(long)
               Species    Attribute value id
1.Sepal.Length  setosa Sepal.Length   5.1  1
2.Sepal.Length  setosa Sepal.Length   4.9  2
3.Sepal.Length  setosa Sepal.Length   4.7  3
4.Sepal.Length  setosa Sepal.Length   4.6  4
5.Sepal.Length  setosa Sepal.Length   5.0  5
6.Sepal.Length  setosa Sepal.Length   5.4  6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2012-09-04
    • 2015-02-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多