[R] how to add a 'label' column

Marc Schwartz marc_schwartz at me.com
Fri Jun 1 22:43:45 CEST 2012


Just to throw out an alternative using the ?reshape function:

# See the example using 'df3' on the help page
DF.Long <- reshape(score, direction = "long", idvar = "subject", 
                   varying = 2:8, sep = "") 


> head(DF.Long, 24)
     subject time    test
ab.1      ab    1 0.17687
cl.1      cl    1 0.18020
ds.1      ds    1 0.14269
ex.1      ex    1 0.14626
ey.1      ey    1 0.16850
js.1      js    1 0.13741
ms.1      ms    1 0.14548
pk.1      pk    1 0.16246
ro.1      ro    1 0.18631
rt.1      rt    1 0.14805
sc.1      sc    1 0.15510
yk.1      yk    1 0.18077
ab.2      ab    2 0.16715
cl.2      cl    2 0.16618
ds.2      ds    2 0.12857
ex.2      ex    2 0.12874
ey.2      ey    2 0.15654
js.2      js    2 0.15516
ms.2      ms    2 0.16655
pk.2      pk    2 0.14872
ro.2      ro    2 0.16128
rt.2      rt    2 0.15794
sc.2      sc    2 0.16374
yk.2      yk    2 0.17320


Regards,

Marc Schwartz


On Jun 1, 2012, at 3:06 PM, Rui Barradas wrote:

> Hello,
> 
> Try
> 
> score2$subject <- rep(score$subject, 7)
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 01-06-2012 20:47, Jason Love escreveu:
>> Hello R users,
>> I'd like to ask a question about how to add a new column. So, below is my
>> situation.
>> 
>> In order to perform the repeated ANOVA, I first imported the following
>> table.
>> 
>> score=read.csv("patients_tests.csv");
>> 
>>    subject     test1     test2     test3       test4    test5    test6
>>   test7
>> 1       ab  0.17687 0.16715 0.17009    0.16480 0.16116 0.24502 0.17975
>> 2       cl   0.18020 0.16618 0.18548    0.14943 0.14289 0.23583 0.17027
>> 3       ds  0.14269 0.12857 0.12972    0.16955 0.14398 0.20865 0.17194
>> 4       ex  0.14626 0.12874 0.15501    0.13019 0.14646 0.21345 0.16023
>> 5       ey  0.16850 0.15654 0.15181    0.18554 0.15642 0.24330 0.16064
>> 6       js   0.13741 0.15516 0.16392    0.15250 0.15416 0.20790 0.15618
>> 7       ms 0.14548 0.16655 0.16561    0.15107 0.14259 0.20386 0.15179
>> 8       pk  0.16246 0.14872 0.15415    0.16553 0.13372 0.25692 0.15096
>> 9       ro   0.18631 0.16128 0.14477    0.16024 0.14063 0.25915 0.19108
>> 10      rt   0.14805 0.15794 0.13665    0.15767 0.13904 0.19171 0.15511
>> 11      sc  0.15510 0.16374 0.16654    0.16864 0.14216 0.20595 0.16847
>> 12      yk  0.18077 0.17320 0.19948    0.15941 0.14053 0.23372 0.18398
>> 
>> Then, I stacked them score2=stack(score);
>> 
>>     values        ind
>> 1  0.17687        test1
>> 2  0.18020        test1
>> 3  0.14269        test1
>> 4  0.14626        test1
>> 5  0.16850        test1
>> 6  0.13741        test1
>> 7  0.14548        test1
>> 8  0.16246        test1
>> 9  0.18631        test1
>> 10 0.14805       test1
>> 11 0.15510       test1
>> 12 0.18077       test1
>> 13 0.16715       test2
>> 14 0.16618       test2
>> 15 0.12857       test2
>>   .
>>   .
>>   .
>> 
>> This gave me a new structure as shown above, but the subject column was not
>> included.
>> So, I'd like to add a new column for subject, but I can't quite figure out
>> the trick.
>> If anyone knows, please help.
>> 
>> Jason



More information about the R-help mailing list