[R] how to add a 'label' column

Jim Lemon jim at bitwrit.com.au
Sat Jun 2 13:14:43 CEST 2012


On 06/02/2012 05:47 AM, Jason Love wrote:
> 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.

Hi Jason,
Perhaps this will do what you want.

library(prettyR)
rep_n_stack(score,to.stack=paste("test",1:7,sep=""))

Jim



More information about the R-help mailing list