[R-sig-eco] Re-arrange data frame

Peter Solymos solymos at ualberta.ca
Fri Jun 29 18:44:51 CEST 2012


Manuel,

You haven't specified the general problem, but for this particular
situation this is how you can do it:

x <- data.frame(array(1:12, c(3,4), list(paste("item", 1:3),
paste("col", 1:4))))
x <- data.frame(Item=rownames(x), x)
y <- data.frame(Item=x$Item[rep(1:3, each=2)],
matrix(as.matrix(x[,-1]), 6, 2, byrow=TRUE))

Cheers,

Peter

--
Péter Sólymos, Dept Biol Sci, Univ Alberta, T6G 2E9, Canada AB
solymos at ualberta.ca, Ph 780.492.8534, http://psolymos.github.com
Alberta Biodiversity Monitoring Institute, http://www.abmi.ca
Boreal Avian Modelling Project, http://www.borealbirds.ca


2012/6/29 Manuel Spínola <mspinola10 at gmail.com>:
> Dear List members,
>
> How can I re-arrange a data frame as detailed below.
>
> old data frame
>
> Item      col1  col2 col3 col4
>
> item1      12    11    6      7
> item2      10    8     5      4
> item3      3      5     4      3
>
> New data frame
>
> item1      12   11
> item1       6     7
> item2      10    8
> item2       5     4
> item3      3      5
> item3      4      3
>
> Best,
>
> Manuel
>
>
> --
> *Manuel Spínola, Ph.D.*
> Instituto Internacional en Conservación y Manejo de Vida Silvestre
> Universidad Nacional
> Apartado 1350-3000
> Heredia
> COSTA RICA
> mspinola at una.ac.cr
> mspinola10 at gmail.com
> Teléfono: (506) 2277-3598
> Fax: (506) 2237-7036
> Personal website: Lobito de río <https://sites.google.com/site/lobitoderio/>
> Institutional website: ICOMVIS <http://www.icomvis.una.ac.cr/>
>
>        [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>



More information about the R-sig-ecology mailing list