[R] collating columns
droberts at montana.edu
Mon Jan 30 23:38:05 CET 2006
You can just use data.frame(). If (using your example) your dataframes
are called first and second, your could
new <- dataframe(first$A,second$Z,first$B,second$Y,first$C,second$X...)
names(new) <- c('A','Z','B','Y','C','X')
If you have an enormous number of columns that's a pain, but it works.
If they're both attached (and the names are unique) you can omit the
"first$" and "second$"
If the dataframes are large, but the spacing is regular, you could do
new <- data.frame(first,second)
column <- c()
for (i in 1:ncol(first)) column <- c(column,i,i+ncol(first))
new <- new[,column]
that way you get columns interspersed in the desired order.
David W. Roberts office 406-994-4548
Professor and Head FAX 406-994-3190
Department of Ecology email droberts at montana.edu
Montana State University
Bozeman, MT 59717-3460
Matthew Scholz wrote:
> Dumb newbie question: I've searched the manual, R help and the mailing list
> archives, but can't seem to find the answer to this simple problem that I
> have. If I have a series of columns in a dataframe: (A, B, C ...) and I want
> to merge them with another series of columns (Z,Y,X ...) such that the
> resulting data frame has columns A,Z,B,Y,C,X ..., how do I do this? In other
> words, I'm trying to collate two column sets (for purposes of writing a
> presentable file) rather than simply using cbind to add one set of columns
> onto the end of another set.
> Thanks in advance of your help.
> Matt Scholz
> Senior Research Specialist
> Department of Plant Sciences
> University of Arizona
> (520) 621-1695
> [[alternative HTML version deleted]]
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help