R/bindData.R

Defines functions bindData

Documented in bindData

bindData <- function(x, y, common)
{
  ## Setup
  if(!is.data.frame(x)) stop("'x' must be a data frame")
  if(!is.data.frame(y)) stop("'y' must be a data frame")

  ## New data frame
  ## First add common column and a dataset indicator column
  z <- rbind(x[common], y[common])

  ## Other columns
  ## Remove common columns in x and y
  namesz <- names(z)
  otherx <- names(x)
  otherx <- otherx[!(otherx %in% namesz)]
  othery <- names(y)
  othery <- othery[!(othery %in% namesz)]

  ## Add all other columns but as a set for each input data frame
  rx <- nrow(x); cx <- length(otherx)
  ry <- nrow(y); cy <- length(othery)

  z <- cbind(z, rbind(x[otherx], matrix(rep(NA, times=(ry * cx)), nrow=ry,
                                        ncol=cx, dimnames=list(NULL, otherx))))
  z <- cbind(z, rbind(matrix(rep(NA, times=(rx * cy)), nrow=rx, ncol=cy,
                             dimnames=list(NULL, othery)), y[othery]))
  z
}

Try the gdata package in your browser

Any scripts or data that you put into this service are public.

gdata documentation built on Oct. 17, 2023, 1:11 a.m.