{portion}
is a small R
package that helps to extract a data portion:
-
works for
vector
,matrix
,data.frame
, andlist
objects -
the relative portion size can be selected
-
allows to select first, last, random, similar or dissimilar data points
-
can portion either row- or column-wise
You can install the released version from CRAN with:
install.packages("portion")
Can portion a vector
:
portion(c(1:5, 51:55), proportion = 0.5, how = "similar")
#> [1] 51 52 53 54 55
#> attr(,"indices")
#> [1] 6 7 8 9 10
portion(1:10, proportion = 0.4, how = "dissimilar", centers = 4)
#> [1] 1 5 8 10
#> attr(,"indices")
#> [1] 1 5 8 10
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
#> 5 0 0 0 0 1 0 0 0
#> 6 0 0 0 0 0 1 0 0
portion(as.data.frame(diag(8)), proportion = 0.3, how = "random", byrow = FALSE)
#> V1 V2 V5
#> 1 1 0 0
#> 2 0 1 0
#> 3 0 0 0
#> 4 0 0 0
#> 5 0 0 1
#> 6 0 0 0
#> 7 0 0 0
#> 8 0 0 0
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