Skip to content

Commit 76451fd

Browse files
committed
Return a numeric data frame from smoothness() on data frames
1 parent 088b0b2 commit 76451fd

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

‎NEWS.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# datawizard (development)
22

3+
BUG FIXES
4+
5+
* `smoothness()` on a data frame now returns a data frame with the columns
6+
`Parameter` and `Smoothness`, as it already did with `iterations`. Before,
7+
without `iterations`, it returned a character matrix, so `as.numeric()` on
8+
the result returned the column names mixed with the values as text.
9+
310
# datawizard 1.5.0
411

512
BREAKING CHANGES

‎R/smoothness.R‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,21 @@ smoothness.data.frame <- function(
7979
...
8080
) {
8181
.smoothness <-
82-
lapply(
83-
x,
84-
smoothness,
85-
method = method,
86-
lag = lag,
87-
iterations = iterations
88-
)
82+
lapply(x, function(i) {
83+
out <- smoothness(
84+
i,
85+
method = method,
86+
lag = lag,
87+
iterations = iterations
88+
)
89+
# without bootstrapping, smoothness() returns a single number; make it
90+
# a data frame like the bootstrapped result, so that `cbind()` below
91+
# does not coerce the values to character
92+
if (!is.data.frame(out)) {
93+
out <- data.frame(Smoothness = as.numeric(out))
94+
}
95+
out
96+
})
8997
.smoothness <- cbind(
9098
Parameter = names(.smoothness),
9199
do.call(rbind, .smoothness)

‎tests/testthat/_snaps/smoothness.md‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
Code
44
smoothness(BOD)
55
Output
6-
Parameter
7-
Time "Time" "0.986393923832144"
8-
demand "demand" "0.406270770677043"
9-
attr(,"class")
10-
[1] "datawizard_smoothness" "parameters_smoothness" "matrix"
11-
[4] "array"
6+
Parameter Smoothness
7+
Time Time 0.9863939
8+
demand demand 0.4062708
129

‎tests/testthat/test-smoothness.R‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ test_that("smoothness works with data frames", {
3434
skip_if(getRversion() < "4.0.0")
3535
set.seed(123)
3636
expect_snapshot(smoothness(BOD))
37+
38+
# invariant: each value equals smoothness() of that column alone
39+
out <- smoothness(BOD)
40+
expect_identical(out$Parameter, c("Time", "demand"))
41+
expect_identical(
42+
as.numeric(out),
43+
c(smoothness(BOD$Time)[[1]], smoothness(BOD$demand)[[1]])
44+
)
3745
})
3846

3947
test_that("smoothness returns datawizard class", {
@@ -46,7 +54,7 @@ test_that("smoothness returns datawizard class", {
4654
)
4755
expect_s3_class(
4856
smoothness(BOD),
49-
c("datawizard_smoothness", "parameters_smoothness", "matrix", "array"),
57+
c("datawizard_smoothness", "parameters_smoothness", "data.frame"),
5058
exact = TRUE
5159
)
5260

0 commit comments

Comments
 (0)