MyNixOS website logo
Description

Extracting a Data Portion.

Provides a simple method to extract portions of a vector, matrix, or data.frame. The relative portion size and the way the portion is selected can be chosen.

Extracting a Data Portion

R-CMD-check CRANstatus Codecov testcoverage

{portion} is a small R package that helps to extract a data portion:

  1. works for vector, matrix, data.frame, and list objects

  2. the relative portion size can be selected

  3. allows to select first, last, random, similar or dissimilar data points

  4. can portion either row- or column-wise

Installation

install.packages("portion")

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("loelschlaeger/portion")

Example

Can portion a vector:

portion(c(1:5, 51:55), proportion = 0.5, how = "similar")
#> [1] 1 2 3 4 5
#> attr(,"indices")
#> [1] 1 2 3 4 5
portion(1:10, proportion = 0.4, how = "dissimilar", centers = 4)
#> [1] 1 3 5 8
#> attr(,"indices")
#> [1] 1 3 5 8

Can portion a matrix:

portion(matrix(LETTERS[1:24], nrow = 4), proportion = 0.5, how = "first")
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] "A"  "E"  "I"  "M"  "Q"  "U" 
#> [2,] "B"  "F"  "J"  "N"  "R"  "V" 
#> attr(,"indices")
#> [1] 1 2
portion(matrix(LETTERS[1:24], nrow = 4), proportion = 0.5, how = "first", byrow = FALSE)
#>      [,1] [,2] [,3]
#> [1,] "A"  "E"  "I" 
#> [2,] "B"  "F"  "J" 
#> [3,] "C"  "G"  "K" 
#> [4,] "D"  "H"  "L" 
#> attr(,"indices")
#> [1] 1 2 3

Can portion a data.frame:

portion(as.data.frame(diag(8)), proportion = 0.3, how = "random")
#>   V1 V2 V3 V4 V5 V6 V7 V8
#> 3  0  0  1  0  0  0  0  0
#> 4  0  0  0  1  0  0  0  0
#> 5  0  0  0  0  1  0  0  0
portion(as.data.frame(diag(8)), proportion = 0.3, how = "random", byrow = FALSE)
#>   V2 V4 V8
#> 1  0  0  0
#> 2  1  0  0
#> 3  0  0  0
#> 4  0  1  0
#> 5  0  0  0
#> 6  0  0  0
#> 7  0  0  0
#> 8  0  0  1

Can work on a list:

portion(list(1:5, diag(3), data.frame(1:3, 2:4)), proportion = 0.5, how = "last")
#> [[1]]
#> [1] 3 4 5
#> attr(,"indices")
#> [1] 3 4 5
#> 
#> [[2]]
#>      [,1] [,2] [,3]
#> [1,]    0    1    0
#> [2,]    0    0    1
#> attr(,"indices")
#> [1] 2 3
#> 
#> [[3]]
#>   X1.3 X2.4
#> 2    2    3
#> 3    3    4
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows