Skip to content
Prev 3167 / 7420 Next

Re-arrange data frame

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>:
_______________________________________________
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
Message-ID: <DDC5EC9B78340042B0D5A0C3789D45690EC9B6EF@001FSN2MPN1-061.001f.mgd2.msft.net>
In-Reply-To: <DDC5EC9B78340042B0D5A0C3789D45690EC9B696@001FSN2MPN1-061.001f.mgd2.msft.net>