It's been a long time since this topic was posted, but I recently had
occasion to use stats::reshape again.  This time, I looked closer at the
code for the function so I could understand what was going on.

I now realize that if the argument "varying" is a vector of names (as I
had) rather than a list (as Dennis had), the reshape function uses "split"
to create the list.  Split in turn uses "as.factor", which sorts the
variables alphanumerically.  So my inputs:
varying=c("y1","x1","y2","x2")
v.names=c("y","x")
are sent to split as:
split(varying,rep(v.names,length(varying)%/%length(v.names)))
which yields the list:
$x
[1] "x1" "x2"
$y
[1] "y1" "y2"
Note the reversal of x and y after split is used.  Unfortunately, when this
happens, the reshape function transposes the data for the "y" and "x"
variables in the long form of the data without any warning.

So to avoid the error I was running into, it is necessary to either make
sure the arguments to varying and v.names are sorted correctly, or to use a
list argument as Dennis has done.

Cheers,
Krishna

On Fri, Aug 6, 2010 at 1:53 PM, Krishna Tateneni <taten...@gmail.com> wrote:

> Thanks for the reply, I realize that having x and y in that order in
> "varying" and "v.names" will work.  The question is why reversing the order
> (i.e., y followed by x) does not work; it seems unintuitive, so I'm
> wondering if I've just misread the documentation.
>
>
> On Fri, Aug 6, 2010 at 1:45 PM, Dennis Murphy <djmu...@gmail.com> wrote:
>
>> Hi:
>>
>> Is this what you were aiming for?
>>
>> reshape(d,varying=list(c("x1","x2"),
>> c("y1","y2")),v.names=c("x","y"),dir="long")
>>     time x y id
>> 1.1    1 1 5  1
>> 2.1    1 2 6  2
>> 1.2    2 3 7  1
>> 2.2    2 4 8  2
>>
>> HTH,
>> Dennis
>>
>> On Fri, Aug 6, 2010 at 10:28 AM, Krishna Tateneni <taten...@gmail.com>wrote:
>>
>>> Hello,
>>>
>>> A quick question for my edification.  When I run the following (R 2.8.1
>>> on
>>> Microsoft Windows):
>>>
>>> > d = data.frame(x1=c(1,2),x2=c(3,4),y1=c(5,6),y2=c(7,8))
>>> > reshape(d,varying=c("y1","x1","y2","x2"),v.names=c("y","x"),dir="long")
>>>
>>> I found myself surprised by the results--the column labeled "y" is
>>> actually
>>> the data from "x1" and "x2", and the column labeled "x" is actually the
>>> data
>>> from "y1" and "y2".
>>>
>>> Is this behaviour of reshape as intended?  That is, have I missed
>>> something
>>> in the documentation?
>>>
>>> Many thanks for any comments.
>>>
>>> --Krishna
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help@r-project.org 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.
>>>
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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