File: left.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 (34 lines) | stat: -rw-r--r-- 917 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
left  <- function(x, n=6L, ...) UseMethod("left")
right <- function(x, n=6L, ...) UseMethod("right")

.leftright <- function(x, n=6L, add.col.nums = TRUE,
                       direction = c("left", "right"), ...)
{
  stopifnot(length(n) == 1L)

  ncx <- ncol(x)

  if (add.col.nums && is.null(colnames(x)))
    colnames(x) <- paste0("[", 1:ncx, ",]")

  if (n < 0L)
    n <- max(ncx + n, 0L)
  else
    n <- min(n, ncx)

  if (direction == "left")
    sel <- seq.int(from=1, length.out = n)
  else  # direction="right"
    sel <- seq.int(to = ncx, length.out = n)

  x[, sel, drop = FALSE]
}

left.data.frame <- function(x, n=6L, add.col.nums=TRUE, ...)
  .leftright(x, n=n, add.col.nums=add.col.nums, direction="left")

right.data.frame <- function(x, n=6L, add.col.nums=TRUE, ...)
  .leftright(x, n=n, add.col.nums=add.col.nums, direction="right")

left.matrix <- left.data.frame
right.matrix <- right.data.frame