| Title: | Extracting a Data Portion |
|---|---|
| Description: | Provides simple methods to extract data portions from various objects. The relative portion size and the way the portion is selected can be chosen. |
| Authors: | Lennart Oelschläger [aut, cre] |
| Maintainer: | Lennart Oelschläger <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.3 |
| Built: | 2026-06-30 19:29:42 UTC |
| Source: | https://github.com/cran/portion |
Extract a relative portion from vectors, matrices, data frames, or lists of these objects.
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 '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, ...)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 '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, ...)
x |
An object to be portioned. |
proportion |
[ The absolute size is rounded up with |
how |
[
Options |
centers |
[ |
... |
Further arguments to be passed to or from other methods. |
byrow |
[ In this case, set to |
ignore |
[ Indices to exclude from the clustering data, but not from the returned
object. With |
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.
# 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 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")