README (original) (raw)

A dependency-free collection of simple functions for cleaning rectangular data. This package allows to detect, count and replace values or discard rows/columns using a predicate function. In addition, it provides tools to check conditions and return informative error messages.

install.packages("arkhe")
# install.packages("remotes")
remotes::install_github("tesselle/arkhe")
## Load the package
library(arkhe)

## Create a matrix
X <- matrix(sample(1:10, 25, TRUE), nrow = 5, ncol = 5)

## Add NA
k <- sample(1:25, 3, FALSE)
X[k] <- NA
X
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   10   10   NA    7    7
#> [2,]    6    4    7   10    3
#> [3,]    7   10    5    9    6
#> [4,]    7   10    4   NA    2
#> [5,]   NA    9    6    9    7

## Count missing values in rows
count(X, f = is.na, margin = 1)
#> [1] 1 0 0 1 1

## Count non-missing values in columns
count(X, f = is.na, margin = 2, negate = TRUE)
#> [1] 4 5 4 4 5

## Find row with NA
detect(X, f = is.na, margin = 1)
#> [1]  TRUE FALSE FALSE  TRUE  TRUE

## Find column without any NA
detect(X, f = is.na, margin = 2, negate = TRUE, all = TRUE)
#> [1] FALSE  TRUE FALSE FALSE  TRUE

## Remove row with any NA
discard(X, f = is.na, margin = 1, all = FALSE)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    6    4    7   10    3
#> [2,]    7   10    5    9    6

## Remove column with any NA
discard(X, f = is.na, margin = 2, all = FALSE)
#>      [,1] [,2]
#> [1,]   10    7
#> [2,]    4    3
#> [3,]   10    6
#> [4,]   10    2
#> [5,]    9    7

## Replace NA with zeros
replace_NA(X, value = 0)
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]   10   10    0    7    7
#> [2,]    6    4    7   10    3
#> [3,]    7   10    5    9    6
#> [4,]    7   10    4    0    2
#> [5,]    0    9    6    9    7

Please note that the arkhe project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.