collating columns

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

collating columns

Matthew Scholz
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
--
Matt Scholz
Senior Research Specialist
Department of Plant Sciences
University of Arizona
(520) 621-1695

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: collating columns

Liaw, Andy
Here's one possible way (assuming the two data frames have the same number
of columns):

> d1 <- data.frame(A=1, B=2, C=3)
> d2 <- data.frame(X=1, Y=2, Z=3)
> res <- c(d1, d2)  # This cbind them and turn into a list.
> idx <- as.vector(matrix(1:(2 * ncol(d1)), 2, byrow=TRUE))
> idx
[1] 1 4 2 5 3 6
> as.data.frame(res[idx])
  A X B Y C Z
1 1 1 2 2 3 3

HTH,
Andy

From: Matthew Scholz

>
> 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
> --
> Matt Scholz
> Senior Research Specialist
> Department of Plant Sciences
> University of Arizona
> (520) 621-1695
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: collating columns

droberts
In reply to this post by Matthew Scholz
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...)

followed by

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.

Dave R.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts                                     office 406-994-4548
Professor and Head                                      FAX 406-994-3190
Department of Ecology                         email [hidden email]
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
> --
> Matt Scholz
> Senior Research Specialist
> Department of Plant Sciences
> University of Arizona
> (520) 621-1695
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html