问题
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