Package {portion}


Type: Package
Title: Extracting a Data Portion
Version: 0.2.0
Description: Extracts a portion of the data in vectors, matrices, data frames, time series, and lists of these objects. The size of the portion is given relative to the size of the object. The portion can consist of the first, the last, random, similar, or dissimilar elements, rows, or columns.
License: GPL (≥ 3)
Encoding: UTF-8
Suggests: testthat (≥ 3.0.0)
Config/testthat/edition: 3
Imports: stats
URL: https://github.com/loelschlaeger/portion
BugReports: https://github.com/loelschlaeger/portion/issues
Config/roxygen2/version: 8.1.0
NeedsCompilation: no
Packaged: 2026-11-12 14:11:28 UTC; loelschlaeger
Author: Lennart Oelschläger [aut, cre]
Maintainer: Lennart Oelschläger <oelschlaeger.lennart@gmail.com>
Repository: CRAN
Date/Publication: 2026-11-12 14:40:02 UTC

portion: Extracting a Data Portion

Description

logo

Extracts a portion of the data in vectors, matrices, data frames, time series, and lists of these objects. The size of the portion is given relative to the size of the object. The portion can consist of the first, the last, random, similar, or dissimilar elements, rows, or columns.

Author(s)

Maintainer: Lennart Oelschläger oelschlaeger.lennart@gmail.com

Authors:

See Also

Useful links:


Extract a data portion

Description

Extract a relative portion from vectors, matrices, data frames, time series, or lists of these objects.

Usage

portion(x, proportion, how = "random", centers = 2L, ...)

## Default S3 method:
portion(x, ...)

## S3 method for class 'numeric'
portion(x, proportion, how = "random", centers = 2L, ...)

## S3 method for class 'ts'
portion(x, proportion, how = "random", centers = 2L, byrow = TRUE, ...)

## S3 method for class 'character'
portion(x, proportion, how = "random", ...)

## S3 method for class 'logical'
portion(x, proportion, how = "random", centers = 2L, ...)

## S3 method for class 'matrix'
portion(
  x,
  proportion,
  how = "random",
  centers = 2L,
  byrow = TRUE,
  ignore = integer(),
  ...
)

## S3 method for class 'data.frame'
portion(
  x,
  proportion,
  how = "random",
  centers = 2L,
  byrow = TRUE,
  ignore = integer(),
  ...
)

## S3 method for class 'list'
portion(x, proportion, how = "random", centers = 2L, ...)

Arguments

x

An object to be portioned.

proportion

[numeric(1)]
The relative portion size as a number between 0 and 1.

The absolute size is the product of proportion and the number of elements, rows, or columns, rounded up to a whole number (e.g., a proportion of 0.5 selects three of five elements).

how

[character(1)]
Exactly one of:

  • "random" (default), select at random

  • "first", select the first elements, rows, or columns

  • "last", select the last elements, rows, or columns

  • "similar", select similar elements, rows, or columns

  • "dissimilar", select dissimilar elements, rows, or columns

Options "similar" and "dissimilar" are based on clustering, see details.

centers

[integer(1)]
Only relevant if how = "similar" or how = "dissimilar". A positive whole number passed to kmeans for clustering (the number of clusters). If centers exceeds the number of distinct values or rows, it is reduced automatically.

...

Further arguments to be passed to or from other methods.

byrow

[logical(1)]
Only relevant if x has two dimensions (rows and columns).

In this case, set to TRUE to portion row-wise (default) or FALSE to portion column-wise.

ignore

[integer()]
Only relevant for two-dimensional x and how = "similar" or how = "dissimilar".

Indices to exclude from the clustering data, but not from the returned object. With byrow = TRUE, these are column indices. With byrow = FALSE, these are row indices. This is useful, for example, to ignore identifier or non-numeric columns while selecting similar rows.

Details

A portion of a vector consists of some of its elements, while a portion of a matrix or data frame consists of some of its rows (or columns if byrow = FALSE). The selected elements, rows, or columns keep their original order. For a list, each element is portioned separately, such that the selected indices can differ between the elements of the list.

Value

A portion of x, preserving attributes where possible. Vectors return selected elements, matrices and data frames return selected rows or columns, and lists return a list with each element portioned. The selected indices are stored in the "indices" attribute of each returned object.

Similar and dissimilar elements

Options how = "similar" and how = "dissimilar" are based on clustering the elements, rows, or columns via kmeans, which requires numeric or logical data without NA, NaN, or infinite values. Note that the clustering starts from randomly chosen centers, hence the result depends on the state of the random number generator (see set.seed).

Based on the clustering, the portion is selected as follows:

Examples

# can portion vectors, matrices, data.frames, and lists of such types
portion(
  list(
    1:10,
    matrix(LETTERS[1:12], nrow = 3, ncol = 4),
    data.frame(a = 1:6, b = -6:-1)
  ),
  proportion = 0.5,
  how = "first"
)

# can portion similar and dissimilar elements (based on kmeans clustering)
set.seed(1)
x <- c(1, 1, 2, 2)
portion(x, proportion = 0.5, how = "similar")
portion(x, proportion = 0.5, how = "dissimilar")

# ignore non-numeric columns when clustering data frame rows
x <- data.frame(value = c(1, 1, 5, 5), group = c("a", "a", "b", "b"))
portion(x, proportion = 0.5, how = "similar", ignore = 2)

# object attributes are preserved
x <- structure(1:10, "test_attribute" = "test")
x[1:5]
portion(x, proportion = 0.5, how = "first")

# can portion time series
portion(stats::ts(1:8, start = 2000), proportion = 0.5, how = "first")

mirror server hosted at Truenetwork, Russian Federation.