Skip to content

Commit d85088c

Browse files
authored
data_replicate will fail with invalid 'times' value when only one column is left in data frame (#654)
* data_replicate will fail with `invalid 'times'` value when only one column is left in data frame Fixes #652 * prevent simplification * don's simplify * data_unique * ups...
1 parent 3f35d37 commit d85088c

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: datawizard
33
Title: Easy Data Wrangling and Statistical Transformations
4-
Version: 1.2.0.5
4+
Version: 1.2.0.6
55
Authors@R: c(
66
person("Indrajeet", "Patil", , "patilindrajeet.science@gmail.com", role = "aut",
77
comment = c(ORCID = "0000-0003-1995-6531")),

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ BUG FIXES
3333
`values_from` when IDs were not balanced (equally spread across observations)
3434
(#644).
3535

36+
* Fixed issue in `data_replicate()` when data frame had only one column to
37+
replicate (#654).
38+
3639
# datawizard 1.2.0
3740

3841
BREAKING CHANGES

R/data_replicate.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ data_replicate <- function(data,
9898
}
9999

100100
# remove rows where "expand" is NA
101-
data <- data[!is.na(replicates), ]
101+
data <- data[!is.na(replicates), , drop = FALSE]
102102
replicates <- replicates[!is.na(replicates)]
103103

104104
# fin

R/data_tabulate.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ as.table.datawizard_table <- function(x, remove_na = TRUE, simplify = FALSE, ver
519519
insight::format_alert("Removing NA values from frequency table.")
520520
}
521521
# remove NA values from the table
522-
x <- x[!is.na(x$Value), ]
522+
x <- x[!is.na(x$Value), , drop = FALSE]
523523
}
524524
# coerce to table
525525
result <- as.table(stats::setNames(x[["N"]], x$Value))

R/data_to_long.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ data_to_long <- function(data,
315315
}
316316

317317
if (values_drop_na) {
318-
out <- out[!is.na(out[, values_to]), ]
318+
out <- out[!is.na(out[, values_to]), , drop = FALSE]
319319
}
320320

321321
# add back attributes

R/data_unique.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ data_unique.data.frame <- function(data,
8080
), ]
8181

8282
good.dups <- data_select(good.dups, og.names)
83-
out <- data[!duplicated(data$temporary_id2), ]
83+
out <- data[!duplicated(data$temporary_id2), , drop = FALSE]
8484

8585

8686
if (keep != "first") {

tests/testthat/test-data_replicate.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,18 @@ test_that("data_replicate: errors", {
5252
d$carb[3] <- Inf
5353
expect_error(data_replicate(d, "carb"), regex = "infinite values")
5454
})
55+
56+
57+
test_that("data_replicate: don't simplify if only one column left", {
58+
a <- c(1, 2, 3, 4)
59+
b <- c(4, 3, 2, 1)
60+
nrtimes <- c(1, 2, 0, 1)
61+
62+
d <- data.frame(a, b, nrtimes)
63+
out <- data_replicate(d, expand = "nrtimes")
64+
expect_identical(dim(out), c(4L, 2L))
65+
66+
d <- data.frame(a, nrtimes)
67+
out <- data_replicate(d, expand = "nrtimes")
68+
expect_identical(dim(out), c(4L, 1L))
69+
})

0 commit comments

Comments
 (0)