[R] 'splice' two data frames

David Whiting david.whiting at ncl.ac.uk
Thu Aug 25 11:02:19 CEST 2005


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.




More information about the R-help mailing list