What code does a task like the reshape2 package in a base reshape function?

不羁的心 提交于 2021-01-29 06:00:45

问题


I'm learning reshape function(base R) that works like reshape2 package of hadley wickham. I wrote the code below using reshape2 package.

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

The result : column names are Species , variable, value and entire number of the result is 600

I wrote this code

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

But show error message

Error in reshape(iris, idvar = "Species", direction = "long") : "no 'reshapeWide' attribute, must specify 'varying'

How to see the same result that perform melt function in reshape2 package?


回答1:


Specify at least varying and preferably all those shown:

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

The first few rows of long are:

> 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


来源:https://stackoverflow.com/questions/57638480/what-code-does-a-task-like-the-reshape2-package-in-a-base-reshape-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!