File: cbindX.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 (36 lines) | stat: -rw-r--r-- 829 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
29
30
31
32
33
34
35
36
cbindX <- function(...)
{
  ## Setup
  x <- list(...)

  ## Are all objects matrices or data.frames?
  test <- sapply(x, function(z) is.matrix(z) | is.data.frame(z))
  if(any(!test)) stop("only matrices and data.frames can be used")

  ## Get maximum number of rows
  tmp <- sapply(x, nrow)
  maxi <- which.max(tmp)
  test <- tmp < tmp[maxi]

  ## Core
  ## Adding additional 'empty' rows so all objects have same number of rows
  for(i in 1:length(tmp)) {
    if(test[i]) {
      add <- matrix(nrow=tmp[maxi] - tmp[i], ncol=ncol(x[[i]]))
      if(is.data.frame(x[[i]])) {
        add <- as.data.frame(add)
      }
      colnames(add) <- colnames(x[[i]])
      x[[i]] <- rbind(x[[i]], add)
    }
  }

  ## Column-bind all objects
  ret <- x[[1]]
  for(i in 2:length(tmp)) {
    ret <- cbind(ret, x[[i]])
  }

  ## Return
  ret
}