[R-gregmisc-commits] r2073 - in pkg/gdata: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Feb 3 21:13:50 CET 2016
Author: warnes
Date: 2016-02-03 21:13:49 +0100 (Wed, 03 Feb 2016)
New Revision: 2073
Modified:
pkg/gdata/R/first.R
pkg/gdata/man/first.Rd
Log:
Add assignment versions of first() and last()
Modified: pkg/gdata/R/first.R
===================================================================
--- pkg/gdata/R/first.R 2016-02-03 20:00:57 UTC (rev 2072)
+++ pkg/gdata/R/first.R 2016-02-03 20:13:49 UTC (rev 2073)
@@ -1,3 +1,16 @@
# 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, ...)
+
+"first<-" <- function(x, n=1, ..., value )
+{
+ x[1:n] <- value[1:n]
+ x
+}
+
+"last<-" <- function(x, n=1, ..., value )
+{
+ index <- seq( length(x)-n+1, length(x) )
+ x[index] <- value[1:n]
+ x
+}
Modified: pkg/gdata/man/first.Rd
===================================================================
--- pkg/gdata/man/first.Rd 2016-02-03 20:00:57 UTC (rev 2072)
+++ pkg/gdata/man/first.Rd 2016-02-03 20:13:49 UTC (rev 2073)
@@ -1,6 +1,8 @@
\name{first}
\alias{first}
\alias{last}
+\alias{first<-}
+\alias{last<-}
\title{Return first or last element of an object}
\description{
Return first or last element of an object. These functions are convenience
@@ -9,6 +11,8 @@
\usage{
first(x, n=1, ...)
last(x, n=1, ...)
+first(x, n=1, ...) <- value
+last(x, n=1, ...) <- value
}
\arguments{
\item{x}{data object}
@@ -17,6 +21,7 @@
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.}
+ \item{value}{a vector of values to be assigned (should be of length \code{n})}
}
\value{
An object (usually) like 'x' but generally smaller.
@@ -36,20 +41,37 @@
first(v)
last(v)
+first(v) <- 9
+v
+
+last(v) <- 20
+v
+
## and for lists
l <- list(a=1, b=2, c=3)
first(l)
last(l)
+first(l) <- "apple"
+last(l) <- "bannana"
+l
+
## and data.frames
df <- data.frame(a=1:2, b=3:4, c=5:6)
first(df)
last(df)
+first(df) <- factor(c("red","green"))
+last(df) <- list(c(20,30)) # note the enclosing list!
+df
+
## and matrixes
m <- as.matrix(df)
first(m)
last(m)
+first(m) <- "z"
+last(m) <- "q"
+m
}
\keyword{ manip }
More information about the R-gregmisc-commits
mailing list