[R] Wide to long form conversion

Gang Chen gangchen6 at gmail.com
Fri Oct 7 13:40:56 CEST 2011


Jim, I really appreciate your help!

I like the power of rep_n_stack, but how can I use rep_n_stack to get
the following result?

  Subj Group value Ref Var Time
1    S1     s     4  Me   F    1
2    S1     s     3  Me   F    2
3    S1     s     5  Me   J    1
4    S1     s     6  Me   J    2
5    S1     s     6 She   F    1
6    S1     s     6 She   F    2
7    S1     s    10 She   J    1
8    S1     s     9 She   J    2

On Fri, Oct 7, 2011 at 7:16 AM, Jim Lemon <jim at bitwrit.com.au> wrote:
> On 10/07/2011 07:28 AM, Gang Chen wrote:
>>
>> I have some data 'myData' in wide form (attached at the end), and
>> would like to convert it to long form. I wish to have five variables
>> in the result:
>>
>> 1) Subj: factor
>> 2) Group: between-subjects factor (2 levels: s / w)
>> 3) Reference: within-subject factor (2 levels: Me / She)
>> 4) F: within-subject factor (2 levels: F1 / F2)
>> 5) J: within-subject factor (2 levels: J1 / J2)
>
> Hi Gang,
> I don't know whether this is the format you want, but:
>
> library(prettyR)
> rep_n_stack(mydata,matrix(c(2,3,6,7,4,5,8,9),nrow=2,byrow=TRUE))
>
> Jim
>



More information about the R-help mailing list