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

Baldwin, Jim -FS jbaldwin at fs.fed.us
Fri Jun 29 19:42:28 CEST 2012


Sorry.  It looked fine when I sent it.  Below I've added back in the line feeds that somehow disappeared.

Jim


-----Original Message-----
From: r-sig-ecology-bounces at r-project.org [mailto:r-sig-ecology-bounces at r-project.org] On Behalf Of Baldwin, Jim -FS
Sent: Friday, June 29, 2012 10:28 AM
To: Peter Solymos; Manuel Spínola
Cc: r-sig-ecology at r-project.org
Subject: Re: [R-sig-eco] Re-arrange data frame

Here is an alternative approach that takes a few more steps and assumes the original data frame is named d0:

# Split into two data frames
d1 <- d0[,c(1:3)]
d2 <- d0[,c(1,4,5)]

# Rename the columns in the second one so that the names match
names(d2) <- names(d1)

# Concatenate the two identically named data frames

 d0.combined <- rbind(d1,d2)

# Sort by Item (assuming it needs to be sorted which matches your example)

 d0.combined <- d0.combined[order(d0.combined$Item),]

Jim Baldwin
USDA Forest Service


-----Original Message-----
From: r-sig-ecology-bounces at r-project.org [mailto:r-sig-ecology-bounces at r-project.org] On Behalf Of Peter Solymos
Sent: Friday, June 29, 2012 9:45 AM
To: Manuel Spínola
Cc: r-sig-ecology at r-project.org
Subject: Re: [R-sig-eco] Re-arrange data frame

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
>

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology




This electronic message contains information generated by the USDA solely for the intended recipients. Any unauthorized interception of this message or the use or disclosure of the information it contains may violate the law and subject the violator to civil or criminal penalties. If you believe you have received this message in error, please notify the sender and delete the email immediately.
_______________________________________________
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