[R] collating columns
Liaw, Andy
andy_liaw at merck.com
Mon Jan 30 22:33:56 CET 2006
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]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list