[R] Code contribution

Андрей Парамонов cmr.pent at gmail.com
Sat Oct 25 20:25:33 CEST 2008


Hello group!

I mantain my own package cmrutils which is available under the GPL:

http[s]://aparamon.msk.ru/svn/study/R-packages/cmrutils

I don't want to put it to CRAN yet because it mostly consists of
specialized helper functions which are presumably not valuable for
other people.

But I think 2 of the functions are general and useful enough to share
the code. Do you think it is worth to include these into one of the
base packages so many people may benefit?

The source code and compiled documentation entries follow.

`empty` <-
function(x)
{
  result <- x
  result[] <- NA
  return(result)
}

Helper Routine

Description:

     Creates empty copy of object.

Usage:

     empty(x)

Arguments:

       x: An object to create copy from.

Value:

     Returns empty (filled with 'NA's) object having the same structure
     and attributes as 'x'.

Examples:

       empty(0)
       empty(1:10)
       empty(matrix(1:4, 2, 2))
       empty(array(1:24, dim = c(2, 3, 4)))

       empty(list(a = 1, b = 2))
       empty(data.frame(a = 1:2, b = 3:4))

       empty(ts(1:10, frequency = 5))

`slice` <-
function(x, MARGIN, n)
{
  result <- x[slice.index(x, MARGIN) == n]
  if(length(result) == prod(dim(x)[-MARGIN]))
  {
    dim(result) <- dim(x)[-MARGIN]
    dimnames(result) <- dimnames(x)[-MARGIN]
  }
  return(result)
}

Helper Routine

Description:

     Slices array by specified dimension.

Usage:

     slice(x, MARGIN, n)

Arguments:

       x: An array-like object.

  MARGIN: An integer giving the dimension to slice by.

       n: Number of slice.

Value:

     Returns array of dimensions 'dim(x)[-MARGIN]'. The corresponding
     dimnames are preserved.

See Also:

     'slice.index'.

Examples:

       x <- 1:10
       slice(x, 1, 1)

       x <- matrix(1:4, 2, 2)
       rownames(x) <- c("Row 1", "Row 2")
       colnames(x) <- c("Col 1", "Col 2")
       x
       slice(x, 1, 1)
       slice(x, 1, 2)
       slice(x, 2, 1)
       slice(x, 2, 2)

       x <- array(1:24, dim = c(2, 3, 4))
       dimnames(x) <- list(letters[1:2], letters[3:5], letters[6:9])
       x
       slice(x, 1, 1)
       slice(x, 2, 1)
       slice(x, 3, 1)

Andrey

--please do not edit the information below--

Version:
 platform = i486-pc-linux-gnu
 arch = i486
 os = linux-gnu
 system = i486, linux-gnu
 status =
 major = 2
 minor = 8.0
 year = 2008
 month = 10
 day = 20
 svn rev = 46754
 language = R
 version.string = R version 2.8.0 (2008-10-20)

Locale:
LC_CTYPE=ru_RU.UTF-8;LC_NUMERIC=C;LC_TIME=ru_RU.UTF-8;LC_COLLATE=ru_RU.UTF-8;LC_MONETARY=C;LC_MESSAGES=ru_RU.UTF-8;LC_PAPER=ru_RU.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=ru_RU.UTF-8;LC_IDENTIFICATION=C

Search Path:
 .GlobalEnv, package:stats, package:graphics, package:grDevices,
package:utils, package:datasets, package:methods, Autoloads,
package:base



More information about the R-help mailing list