## This file contains code taken from dplyr. The license and copyright is:
## The MIT License (MIT)
## =====================
## Copyright © 2013-2019 RStudio and others.
## Permission is hereby granted, free of charge, to any person
## obtaining a copy of this software and associated documentation
## files (the “Software”), to deal in the Software without
## restriction, including without limitation the rights to use,
## copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following
## conditions:
## The above copyright notice and this permission notice shall be
## included in all copies or substantial portions of the Software.
## THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
## OTHER DEALINGS IN THE SOFTWARE.
context("Mutate...")
test_that("grouped mutate... does not drop grouping attributes", {
d <- data.frame(subject = c("Jack", "Jill"), id = c(2, 1)) %>%
dplyr::group_by(subject)
a1 <- names(attributes(d))
a2 <- names(attributes(d %>% mutate...(foo = 1)))
expect_equal(setdiff(a1, a2), character(0))
})
test_that("repeated outputs applied progressively (data frame)", {
df <- data.frame(x = 1)
out <- mutate...(df, z = x + 1, z = z + 1)
expect_equal(nrow(out), 1)
expect_equal(ncol(out), 2)
expect_equal(out$z, 3)
})
test_that("repeated outputs applied progressively (grouped_df)", {
df <- data.frame(x = c(1, 1), y = 1:2)
ds <- dplyr::group_by(df, y)
out <- mutate...(ds, z = x + 1, z = z + 1)
expect_equal(nrow(out), 2)
expect_equal(ncol(out), 3)
expect_equal(out$z, c(3L, 3L))
})
## Window fn testing
test_that("desc is correctly handled by window functions", {
df <- data.frame(
x = 1:10, y = seq(1, 10, by = 1),
g = rep(c(1, 2), each = 5), s = c(letters[1:3], LETTERS[1:5], letters[4:5])
)
expect_equal(mutate...(df,
rank = dplyr::min_rank(dplyr::desc(x)))$rank, 10:1)
expect_equal(mutate...(dplyr::group_by(df, g),
rank = dplyr::min_rank(dplyr::desc(x)))$rank, rep(5:1, 2))
expect_equal(mutate...(df,
rank = dplyr::row_number(dplyr::desc(x)))$rank, 10:1)
expect_equal(mutate...(dplyr::group_by(df, g),
rank = dplyr::row_number(dplyr::desc(x)))$rank, rep(5:1, 2))
# Test character vector sorting
charvec_sort_test <- function(df) {
expect_equal(
mutate...(df, rank = dplyr::row_number(dplyr::desc(s)))$rank,
mutate...(df, rank = dplyr::row_number(dplyr::desc(s)))$rank
)
expect_equal(
mutate...(dplyr::group_by(df, g), rank = dplyr::row_number(dplyr::desc(s)))$rank,
mutate...(dplyr::group_by(df, g), rank = dplyr::row_number(dplyr::desc(s)))$rank
)
}
# Test against both the local, and the C locale for collation
charvec_sort_test(df)
withr::with_collate("C", charvec_sort_test(df))
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.