Skip to content

Commit 284d9aa

Browse files
committed
fix #5
1 parent 7a57901 commit 284d9aa

5 files changed

Lines changed: 25 additions & 4 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: narray
22
Title: R Package for Handling Arrays in a Consistent Manner
3-
Version: 0.2.1
3+
Version: 0.2.2
44
Author: Michael Schubert <mschu.dev@gmail.com>
55
Maintainer: Michael Schubert <mschu.dev@gmail.com>
66
Description: Provides functions to query and manipulate arrays

R/split.r

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ split = function(X, along, subsets=c(1:dim(X)[along]), drop=FALSE) {
1212
if (along == -1)
1313
along = length(dim(X))
1414

15-
usubsets = unique(subsets)
15+
if (any(is.na(subsets)))
16+
warning("'subsets' contains NA, dropping those values")
17+
18+
usubsets = stats::na.omit(unique(subsets))
1619
lus = length(usubsets)
1720
idxList = base::rep(list(base::rep(list(TRUE), length(dim(X)))), lus)
1821

19-
for (i in 1:lus)
20-
idxList[[i]][[along]] = subsets==usubsets[i]
22+
for (i in 1:lus) {
23+
cur = subsets==usubsets[i]
24+
cur[is.na(cur)] = FALSE
25+
idxList[[i]][[along]] = cur
26+
}
2127

2228
if (length(usubsets)!=dim(X)[along] || !is.numeric(subsets))
2329
lnames = usubsets

R/subset.r

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ subset = function(X, index, along=NULL, drop=FALSE) {
2525
index = tmp
2626
}
2727

28+
if (any(is.na(unlist(index))))
29+
stop("trying to subset with NA in index")
30+
2831
do.call(function(...) `[`(X, ..., drop=drop), index)
2932
}

tests/testthat/test_split.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ test_that("3-dimensional array", {
1717

1818
expect_equal(Y, Yref)
1919
})
20+
21+
test_that("NA in subsets", {
22+
C = structure(c(1L, 2L, 3L, 4L, 6L, 5L), .Dim = 2:3,
23+
.Dimnames = list(c("a", "b"), c("x", "y", "z")))
24+
25+
expect_warning(x <- split(C, along=2, subsets=c(1,1,NA)))
26+
expect_equal(x, list(`1`=C[,c(1:2)]))
27+
})

tests/testthat/test_subset.r

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ test_that("everything", {
2828
subset(C, LETTERS[3], along=2, drop=FALSE),
2929
subset(C, c(FALSE, FALSE, TRUE, FALSE, along=2, drop=FALSE)))
3030
})
31+
32+
test_that("subset with NA", {
33+
expect_error(subset(1:5, index=c(rep(FALSE, 4), NA)))
34+
})

0 commit comments

Comments
 (0)