File: some.R

package info (click to toggle)
car 3.1-5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,496 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 776 bytes parent folder | download | duplicates (5)
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
# adapted from head() and tail()
# 3/10/2017:  S. Weisberg modified to add an argument 'cols'
#             cols = num will display only the first num cols


some <- function(x, ...) UseMethod("some")

some.default <- function(x, n=10, ...){
    len <- length(x)
    ans <- x[sort(sample(len, min(n, len)))]
    if (length(dim(x)) == 1)
        array(ans, n, list(names(ans)))
    else ans
    }

some.matrix <- function(x, n=10, cols=NULL, ...){
  nr <- nrow(x)
  nc <- ncol(x)
  cols <- if(is.null(cols)) 1:nc else cols
  x[sort(sample(nr, min(n, nr))), cols, drop = FALSE]
  }

some.data.frame <- function(x, n=10, cols=NULL, ...){
    nr <- nrow(x)
    nc <- ncol(x)
    cols <- if(is.null(cols)) 1:nc else cols
    x[sort(sample(nr, min(n, nr))), cols, drop=FALSE]
    }