Skip to content

Commit

Permalink
fix when overwriting existing variables in grouped_df (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Jun 14, 2023
1 parent 86bad1b commit e78ae5c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.7.1.11
Version: 0.7.1.12
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
7 changes: 6 additions & 1 deletion R/data_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ data_modify.grouped_df <- function(data, ...) {

# create new variables as dummys, do for-loop works
for (i in names(dots)) {
data[[i]] <- NA
# don't overwrite / fill existing variables with NA,
# e.g. if we have "data_modify(iris, Sepal.Length = normalize(Sepal.Length))"
# normalize() won't work when we fill with NA
if (!i %in% colnames(data)) {
data[[i]] <- NA
}
}

# create new variables per group
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-data_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,18 @@ test_that("data_modify works with character variables, and inside functions", {
tolerance = 1e-3
)
})


test_that("data_modify works with grouped df when overwriting existing variables", {
data(iris)
iris_grp <- data_group(iris, "Species")
out <- data_modify(iris_grp, Sepal.Length = normalize(Sepal.Length))
expect_equal(head(out$Sepal.Length), c(0.53333, 0.4, 0.26667, 0.2, 0.46667, 0.73333), tolerance = 1e-3)

out <- data_modify(
iris_grp,
Sepal.Length = normalize(Sepal.Length),
Sepal.Length2 = 2 * Sepal.Length
)
expect_equal(head(out$Sepal.Length2), 2 * c(0.53333, 0.4, 0.26667, 0.2, 0.46667, 0.73333), tolerance = 1e-3)
})

0 comments on commit e78ae5c

Please sign in to comment.