[R] swutching rows to columns

Ista Zahn istazahn at gmail.com
Tue Mar 23 00:05:38 CET 2010


Hi JR,

t(EmpCo)

gives what you want. See ?t for details.

You can also do this with reshape:

library(reshape)
EmpCo$Label <- rownames(EmpCo)
recast(EmpCo, variable ~ Label, measure.var=c("TRN", "TRN_CLUST"))

but in this case t() seems a bit quicker and easier.

-Ista

On Mon, Mar 22, 2010 at 6:37 PM, LCOG1 <jroll at lcog.org> wrote:
>
> Hi All,
>
> Consider the following:
> TRN<-c(5.809657,3.1, 1.774901e-02)
> TRN_CLUST<-c(-4.174682e-05,  5.538742e-05,1.2)
> EmpCo<-data.frame(TRN,TRN_CLUST)
> row.names(EmpCo)<-c("Slope","Fwy","Univ")
>
> returns:
>            TRN     TRN_CLUST
> Slope 5.80965700 -4.174682e-05
> Fwy   3.10000000  5.538742e-05
> Univ  0.01774901  1.200000e+00
>
> Now my own data is actually first constructed into list form(see below) so
> perhaps it would be easier to perform the rows to columns operation from
> that.
>
> List form:
> $TRN
>       Slope            Fwy              UnivDist
>   5.80965700      3.10000000       0.01774901
>
> What i would like to do is switch the rows to columsn so that the above now
> shows:
>
>                  Slope         Fwy                Univ
> TRN               5.809657      3.1             0.01774901
> TRN_CLUST -4.17E-05     5.54E-05        1.20E+00
>
>
> Tried some things from the reshape package but i dont think thats what i
> want.  I will need to do this for more variables and initial columns than
> shown here so if the process is automated or easily put into an
> automated(loop) form that would be best.  Gracias
>
> JR
> --
> View this message in context: http://n4.nabble.com/swutching-rows-to-columns-tp1678429p1678429.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list