[R] 'splice' two data frames

From: David Whiting <david.whiting_at_ncl.ac.uk>
Date: Thu 25 Aug 2005 - 19:02:19 EST


Hi,

I often need to take columns from two data.frames and 'splice' them together (alternately taking a column from the first data frame, then from the second). For example:

x <- table(sample(letters[1:9], 100, replace=TRUE),

           sample(letters[1:4], 100, replace=TRUE)) y <- prop.table(x)

splice <- function (x, y) {
  z <- matrix(rep(NA, (ncol(x) * 2) * nrow(x)), nrow = nrow(x))   j <- 1
  for (i in seq(1, ncol(z), by = 2)) {
    z[, i] <- x[, j]
    z[, (i + 1)] <- y[, j]
    j <- j + 1
  }
  z <- data.frame(z)
  rownames(z) <- rownames(x)
  z
}

splice(x, y)

Manually using indexing I can do this:

zz <- data.frame(x[, 1], y[, 1], x[, 2], y[, 2], x[, 3], y[, 3], x[, 4], y[, 4])

I *feel* that it should be possible in R to generate the sequence of column indexes automatically. I can get close with this:

i <- paste("x[,", 1:ncol(x), "], ",

	   "y[,", 1:ncol(y), "]",
	   collapse=", ")

which creates a string version of what I want, but I am not sure how to use that with data.frame. FAQ 7.21 ("How can I turn a string into a variable?") looked promising but I have not been able to apply any of the suggestions to this problem. I also tried using do.call:

i <- paste("x[,", 1:4, "],", "y[,", 1:4, "]", collapse=",")
i <- gsub("],", "]@", i)  # Create a marker for
i <- strsplit(i, "@")     # strsplit to create a list
do.call(data.frame, i)

and with lapply:

lappy(i, data.frame)

These "did not work" (i.e. they worked as they were designed to and did not give me the results I am after).

I think I need a nudge or two in the right direction.

Thanks.

Dave

-- 
David Whiting
School of Clinical Medical Sciences, The Medical School
University of Newcastle upon Tyne, NE2 4HH, UK.

______________________________________________
R-help@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
Received on Thu Aug 25 19:08:49 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:55 EST