[R-gregmisc-users] SF.net SVN: r-gregmisc:[1978] trunk/gdata
Brought to you by:
warnes
From: <wa...@us...> - 2015-04-28 04:44:11
|
Revision: 1978 http://sourceforge.net/p/r-gregmisc/code/1978 Author: warnes Date: 2015-04-28 04:44:08 +0000 (Tue, 28 Apr 2015) Log Message: ----------- - first() and last() are now simply wrappers to utils::head() and utils::tail() with a default 'n=1' instead of 'n=6'. - Move code for left() and right() into a separate file. Modified Paths: -------------- trunk/gdata/R/first.R trunk/gdata/man/first.Rd Added Paths: ----------- trunk/gdata/R/left.R Modified: trunk/gdata/R/first.R =================================================================== --- trunk/gdata/R/first.R 2015-04-28 04:41:39 UTC (rev 1977) +++ trunk/gdata/R/first.R 2015-04-28 04:44:08 UTC (rev 1978) @@ -1,30 +1,3 @@ -first <- function(x) UseMethod("first") -last <- function(x) UseMethod("last") - -left <- function(x, n=6) UseMethod("left") -right <- function(x, n=6) UseMethod("left") - - -first.default <- function(x) x[1] -last.default <- function(x) x[length(x)] - - -first.list <- function(x) x[[1]] -last.list <- function(x) x[[length(x)]] - - -left.data.frame <- function(x, n=6) -{ - n <- min(n, ncol(x)) - x[, 1:n] -} -left.matrix <- left.data.frame - - -right.data.frame <- function(x, n=6) -{ - n <- min(n, ncol(x)) - x[, (ncol(x)-n+1):ncol(x)] -} -right.matrix <- right.data.frame - +# Simply call 'first' or 'last' with a different default value for 'n'. +first <- function(x, n=1, ...) head(x, n=n, ...) +last <- function(x, n=1, ...) tail(x, n=n, ...) Added: trunk/gdata/R/left.R =================================================================== --- trunk/gdata/R/left.R (rev 0) +++ trunk/gdata/R/left.R 2015-04-28 04:44:08 UTC (rev 1978) @@ -0,0 +1,38 @@ +left <- function(x, n=6L) UseMethod("left") +right <- function(x, n=6L) UseMethod("left") + +left.data.frame <- function(x, n=6) +{ + stopifnot(length(n) == 1L) + n <- if (n < 0L) + max(ncol(x) + n, 0L) + else min(n, ncol(x)) + x[, seq_len(n), drop = FALSE] +} +left.matrix <- left.data.frame + + +right.data.frame <- function (x, n = 6L, ...) +{ + stopifnot(length(n) == 1L) + ncx <- ncol(x) + n <- if (n < 0L) + max(ncx + n, 0L) + else min(n, ncx) + x[, seq.int(to = ncx, length.out = n), drop = FALSE] +} + +right.matrix <- function (x, n = 6L, addcolnums = TRUE, ...) +{ + stopifnot(length(n) == 1L) + ncx <- ncol(x) + n <- if (n < 0L) + max(ncx + n, 0L) + else min(n, ncx) + sel <- seq.int(to = ncx, length.out = n) + ans <- x[, sel, drop = FALSE] + if (addcolnums && is.null(colnames(x))) + colnames(ans) <- paste0("[", sel, ",]") + ans +} + Modified: trunk/gdata/man/first.Rd =================================================================== --- trunk/gdata/man/first.Rd 2015-04-28 04:41:39 UTC (rev 1977) +++ trunk/gdata/man/first.Rd 2015-04-28 04:44:08 UTC (rev 1978) @@ -1,28 +1,25 @@ \name{first} \alias{first} -\alias{first.default} -\alias{first.list} \alias{last} -\alias{last.default} -\alias{last.list} -\title{Return first or last element of a vector or list} +\title{Return first or last element of an object} \description{ - Return first or last element of a vector or list -} + Return first or last element of an object. These functions are convenience + wrappers for \code{head(x, n=1, ...)} and \code{tail(x, n=1, ...)}. + } \usage{ -first(x) -last(x) -\method{first}{default}(x) -\method{last}{default}(x) -\method{first}{list}(x) -\method{last}{list}(x) +first(x, n=1, ...) +last(x, n=1, ...) } -%- maybe also 'usage' for other objects documented here. \arguments{ - \item{x}{vector or list} + \item{x}{data object} + \item{n}{a single integer. If positive, size for the resulting object: + number of elements for a vector (including lists), rows for a + matrix or data frame or lines for a function. If negative, + all but the ‘n’ last/first number of elements of ‘x’.} + \item{...}{arguments to be passed to or from other methods.} } \value{ - The first or last element of \code{x}. + An object (usually) like ‘x’ but generally smaller. } \author{ Gregory R. Warnes \email{gr...@wa...} @@ -43,5 +40,16 @@ l <- list(a=1, b=2, c=3) first(l) last(l) + +## and data.frames +df <- data.frame(a=1:2, b=3:4, c=5:6) +first(df) +last(df) + +## and matrixes +m <- as.matrix(df) +first(m) +last(m) + } \keyword{ manip } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |