File: bindData.R

package info (click to toggle)
gdata 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: sh: 27; makefile: 15
file content (28 lines) | stat: -rw-r--r-- 932 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
}