@@ -2,15 +2,11 @@ context("stack")
22
33A = matrix (1 : 4 , nrow = 2 , ncol = 2 , dimnames = list (c(' a' ,' b' ),c(' x' ,' y' )))
44B = matrix (5 : 6 , nrow = 2 , ncol = 1 , dimnames = list (c(' b' ,' a' ),' z' ))
5-
6- C = stack(list (A , B ), along = 2 )
7- # x y z
8- # a 1 3 6 # B is stacked correctly according to its names
9- # b 2 4 5
5+ C = structure(c(1L , 2L , 3L , 4L , 6L , 5L ), .Dim = 2 : 3 ,
6+ .Dimnames = list (c(" a" , " b" ), c(" x" , " y" , " z" )))
107
118test_that(" match names, not extent" , {
12- Cref = structure(c(1L , 2L , 3L , 4L , 6L , 5L ), .Dim = 2 : 3 ,
13- .Dimnames = list (c(" a" , " b" ), c(" x" , " y" , " z" )))
9+ Cref = stack(list (A , B ), along = 2 )
1410 expect_equal(C , Cref )
1511})
1612
@@ -76,6 +72,23 @@ test_that("1-row/col matrix stacking", {
7672 expect_equal(stack(list (B = t(B )), along = 2 ), t(B ))
7773})
7874
75+ test_that(" allow overwrite" , {
76+ ov = A
77+ ov [1 ,1 ] = 10
78+ expect_error(stack(ov , A , along = 2 ))
79+ expect_error(stack(A , ov , along = 2 ))
80+ expect_equal(stack(ov , A , along = 2 , allow_overwrite = TRUE ), A )
81+ expect_equal(stack(A , ov , along = 2 , allow_overwrite = TRUE ), ov )
82+ expect_equal(stack(A , A , along = 2 ), A )
83+
84+ ov [] = NA
85+ expect_equal(stack(A , ov , along = 2 ), A )
86+ expect_equal(stack(ov , A , along = 2 ), A )
87+ ov [] = 0
88+ expect_equal(stack(A , ov , along = 2 , fill = 0 ), A )
89+ expect_equal(stack(ov , A , along = 2 , fill = 0 ), A )
90+ })
91+
7992test_that(" keep_empty arg when stacking zero-length vectors" , {
8093 a = setNames(1 : 3 , letters [1 : 3 ])
8194 b = numeric ()
@@ -91,15 +104,6 @@ test_that("keep_empty arg when stacking zero-length vectors", {
91104 expect_equal(re2 , t(re3 ))
92105})
93106
94- test_that(" allow overwrite" , {
95- ov = A
96- ov [1 ,1 ] = 10
97- expect_error(stack(ov , A , along = 2 ))
98- expect_error(stack(A , ov , along = 2 ))
99- expect_equal(stack(ov , A , along = 2 , allow_overwrite = TRUE ), A )
100- expect_equal(stack(A , ov , along = 2 , allow_overwrite = TRUE ), ov )
101- })
102-
103107test_that(" performance" , {
104108 skip_on_cran()
105109
0 commit comments