Hi Henrik,
I don't think it's the attributes that are a problem - it's that that
column is a matrix.
Hadley
On Fri, Jul 27, 2012 at 11:20 AM, Henrik Singmann <
sing...@gmail.com> wrote:
> Hi all,
>
> today I noticed a strange behavior using reshape2. Namely that it gives an
> error while melting a data.frame with a numeric id.vars that has an
> attribute. This does not happen with a factor that as attributes. And it
> totally works with reshape.
> The following code illustrates the problem:
>
> require(reshape2)
>
> # wide data.frame with a factor, a numeric covariate (num) and two
> responses:
> df1 <- data.frame(condition = c("A", "B"), num = rnorm(2), x = rnorm(2), y =
> rnorm(2))
> str(df1)
> # 'data.frame': 2 obs. of 4 variables:
> # $ condition: Factor w/ 2 levels "A","B": 1 2
> # $ num : num 0.661 -0.404
> # $ x : num -0.26 0.181
> # $ y : num -0.478 0.599
>
> melt(df1, id.vars = c("condition", "num")) # works
> # condition num variable value
> # 1 A 0.6611 x -0.2600
> # 2 B -0.4044 x 0.1812
> # 3 A 0.6611 y -0.4779
> # 4 B -0.4044 y 0.5987
>
> # add attributes to the covariate
> df2 <- within(df1, num <- scale(num))
> str(df2)
> # 'data.frame': 2 obs. of 4 variables:
> # $ condition: Factor w/ 2 levels "A","B": 1 2
> # $ num : num [1:2, 1] 0.707 -0.707
> # ..- attr(*, "scaled:center")= num 0.128
> # ..- attr(*, "scaled:scale")= num 0.753
> # $ x : num -0.26 0.181
> # $ y : num -0.478 0.599
>
> melt(df2, id.vars = c("condition", "num")) # gives an error
> # Fehler in data.frame(ids, variable, value, stringsAsFactors = FALSE) :
> # arguments imply differing number of rows: 2, 4
>
> # remove attributes, works again:
> df3 <- within(df2, num <- factor(num))
> str(df3)
> # 'data.frame': 2 obs. of 4 variables:
> # $ condition: Factor w/ 2 levels "A","B": 1 2
> # $ num : Factor w/ 2 levels "-0.707106781186548",..: 2 1
> # $ x : num -0.26 0.181
> # $ y : num -0.478 0.599
>
> melt(df3, id.vars = c("condition", "num")) #works
> # condition num variable value
> # 1 A 0.707106781186548 x -0.2600
> # 2 B -0.707106781186548 x 0.1812
> # 3 A 0.707106781186548 y -0.4779
> # 4 B -0.707106781186548 y 0.5987
>
> # add attributes to the factor:
> contrasts(df3$condition) <- contr.sum
> str(df3)
> #'data.frame': 2 obs. of 4 variables:
> # $ condition: Factor w/ 2 levels "A","B": 1 2
> # ..- attr(*, "contrasts")= num [1:2, 1] 1 -1
> # .. ..- attr(*, "dimnames")=List of 2
> # .. .. ..$ : chr "A" "B"
> # .. .. ..$ : NULL
> # $ num : Factor w/ 2 levels "-0.707106781186548",..: 2 1
> # $ x : num -0.26 0.181
> # $ y : num -0.478 0.599
>
> melt(df3, id.vars = c("condition", "num")) # works
> # condition num variable value
> # 1 A 0.707106781186548 x -0.2600
> # 2 B -0.707106781186548 x 0.1812
> # 3 A 0.707106781186548 y -0.4779
> # 4 B -0.707106781186548 y 0.5987
>
> As said, everything works with reshape and it does not seem to be intended
> behavior. Or am I wrong?
>
> Cheers,
> Henrik
>
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/manipulatr/-/pD1N0A5ZwWgJ.
> To post to this group, send email to
manip...@googlegroups.com.
> To unsubscribe from this group, send email to
>
manipulatr+...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/manipulatr?hl=en.
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/