If I try to reshape a data frame into a long format with more than one
column it seems to mix up the column headings (or the columns, depending on
how you look at it). For example:
> d <-
data.frame(row=1:2,b.1=c("1b1","2b1"),b.2=c("1b2","2b2"),a.1=c("1a1","2a1"),a.2=c("1a2","2a2"))
> d
  row b.1 b.2 a.1 a.2
1   1 1b1 1b2 1a1 1a2
2   2 2b1 2b2 2a1 2a2

If I try reshape on this, the column headings are over the wrong columns:
> reshape(d,direction="long",idvar="row",varying=2:5)
    row time   b   a
1.1   1    1 1a1 1b1
2.1   2    1 2a1 2b1
1.2   1    2 1a2 1b2
2.2   2    2 2a2 2b2

If I reorder the columns, so the "a" columns come first, it reverses the
column names and gives the right result.
> e <- d[,c(1,4,5,2,3)]
> reshape(e,direction="long",idvar="row",varying=2:5)
    row time   a   b
1.1   1    1 1a1 1b1
2.1   2    1 2a1 2b1
1.2   1    2 1a2 1b2
2.2   2    2 2a2 2b2

I can also get the right result if I specify the "varying" parameter more
explicitly:
>
reshape(d,direction="long",idvar="row",varying=list(names(d)[2:3],names(d)[4:5]))
    row time b.1 a.1
1.1   1    1 1b1 1a1
2.1   2    1 2b1 2a1
1.2   1    2 1b2 1a2
2.2   2    2 2b2 2a2

I'm using R 2.4.1 on Windows XP.

Am I doing something wrong? Is it supposed to work like this?

Thanks, Ian

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to